2012-12-26 50 views
-1

我目前正在考慮一些自動化來讀取網頁數據。那麼是否可以閱讀以下類型的表格以便從網頁中讀取excel:excel的值應該爲name of condion,Operator and Expressions是否可以讀取網頁的html表格數據?

編輯

>>> from urllib import urlopen 
>>> from bs4 import BeautifulSoup 
>>> source = BeautifulSoup(urlopen(url)) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
NameError: name 'url' is not defined 
>>> source = BeautifulSoup(urlopen(https://demo.aravo.com)) 
    File "<stdin>", line 1 
    source = BeautifulSoup(urlopen(https://demo.aravo.com)) 
             ^
SyntaxError: invalid syntax 
>>> from urllib import urlopen 
>>> from bs4 import BeautifulSoup 
>>> source = BeautifulSoup(urlopen(https://demo.aravo.com/)) 
    File "<stdin>", line 1 
    source = BeautifulSoup(urlopen(https://demo.aravo.com/)) 
             ^
SyntaxError: invalid syntax 
>>> source = BeautifulSoup(urlopen(demo.aravo.com/)) 
    File "<stdin>", line 1 
    source = BeautifulSoup(urlopen(demo.aravo.com/)) 
               ^
SyntaxError: invalid syntax 
>>> source = BeautifulSoup(urlopen(demo.aravo.com)) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
NameError: name 'demo' is not defined 
>>> 

EDIT2

C:\Users>cd.. 

C:\>cd cd C:\Python27\selenv\Scripts 
The filename, directory name, or volume label syntax is incorrect. 

C:\>cd C:\Python27\selenv\Scripts 

C:\Python27\selenv\Scripts>python 
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win 
32 
Type "help", "copyright", "credits" or "license" for more information. 
>>> from urllib import urlopen 
>>> from bs4 import BeautifulSoup 
>>> source = BeautifulSoup(urlopen("https://demo.aravo.com/")) 
>>> tables = source.findAll('td') 
>>> import csv 
>>> writer = csv.writer(open('filename.csv','w')) 
>>> writer.writerow(rows) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
NameError: name 'rows' is not defined 
>>> 

感謝

+2

看起來你希望我們爲你寫一些代碼。儘管許多用戶願意爲遇險的編碼人員編寫代碼,但他們通常只在海報已嘗試自行解決問題時才提供幫助。證明這一努力的一個好方法是包含迄今爲止編寫的代碼,示例輸入(如果有的話),期望的輸出和實際獲得的輸出(控制檯輸出,堆棧跟蹤,編譯器錯誤 - 無論是適用)。您提供的細節越多,您可能會收到的答案就越多。 –

+4

簡短的回答:肯定有可能,你有什麼嘗試? –

+0

任何人都可以幫助我在這裏開始? – CodeLover

回答

1

這是可能的,檢查所謂的美麗的湯庫,這將簡化後歌廳正確信息的過程你刪掉了頁面

#!/usr/bin/env python 
from selenium import webdriver 

browser = webdriver.Firefox() 
url = 'http://python.org' 
browser.get(url) 
page_source = browser.page_source 
print page_source 
+0

我剛剛在我的電腦中安裝了python 2.7和selenium。現在試圖爲此編寫一些代碼。但我無法框架! – CodeLover

+0

檢查我的更新 – Vor

+0

意味着你要求在本地驅動器中保存頁面源代碼,對吧? – CodeLover

1

你也可以使用的urlopen從urllib的庫來獲得網頁源代碼,然後BeautifulSoup解析HTML

from urllib import urlopen 

from beautifulSoup import BeautifulSoup 

#get BeautifulSoup object 
source = BeautifulSoup(urlopen(url)) 

#get list of table elements from source 
tables = source.findAll('td') 

最簡單的方式將信息保存在EXEL使用它可能是它保存爲.csv文件

您可以在此使用CSV模塊

import csv 
writer = csv.writer(open('filename.csv','w')) 
writer.writerow(rows) 

所有這些模塊做記錄得相當好,你應該能夠填補空白。

爲確保安裝這些庫,請確保您有easy_install,可以通過setuptools進行下載。一旦你這有easy_install的運行型入殼:

easy_install csv 
easy_install BeautifulSoup 
easy_install urllib 
easy_install ipython 

然後運行IPython中進入現場Python環境

ipython 

這將打開一個Python殼它前面的代碼可以進行測試。我希望這有幫助。如果您需要更多基礎知識的幫助,請在網上搜索python教程。 [scraperwiki][3]有一些很好的python網頁解析的例子。

+0

我剛剛安裝了硒和python2.7 - 我相信!我需要爲此安裝什麼或更多? – CodeLover

+1

我對硒不熟悉。我認爲urllib和csv帶有大多數python2.7安裝。你可以使用pip或easy_install來獲得BeautifulSoup。您可以通過輸入'import urllib'等來查看是否有這些或不在您的終端中,並查看是否有任何錯誤。 –

+0

如果沒有提供任何參考,你會建議我安裝它,因爲我是這個平臺的新手,所以在這方面尋求一些建議或幫助! – CodeLover

相關問題