我想從網頁收集數據,有一堆選擇列表,我需要從中獲取 數據。這裏是頁面: - http://www.asusparts.eu/partfinder/Asus/All在一/ E系列/使用python和beautifulsoup的選擇菜單從網頁獲取數據
而且這是我到目前爲止有:
import glob, string
from bs4 import BeautifulSoup
import urllib2, csv
for file in glob.glob("http://www.asusparts.eu/partfinder/*"):
##-page to show all selections for the E-series-##
selected_list = 'http://www.asusparts.eu/partfinder/Asus/All In One/E Series/'
##-
page = urllib2.urlopen(selected_list)
soup = BeautifulSoup(page)
##-page which shows results after selecting one option-##
url = 'http://www.asusparts.eu/partfinder/Asus/All In One/E Series/ET10B'
##-identify the id of select list which contains the E-series-##
select = soup.find('select', id="myselectListModel")
option_tags = select.findAll('option')
##-omit first item in list as isn't part of the option-##
option_tags = option_tags[1:]
for option in option_tags:
open(url + option['value'])
html = urllib2.urlopen("http://www.asusparts.eu/partfinder/")
soup = BeautifulSoup(html)
all = soup.find('div', id="accordion")
我不知道如果我要回到正確的方法是什麼?由於所有的選擇菜單令人困惑。基本上我需要抓取 來自選定結果的所有數據,如圖像,價格,描述等。它們都包含在 一個div標籤中,其中包含所有結果,這些結果被命名爲'accordion',那麼它是否仍然會收集所有數據? 還是我需要深入挖掘這個div內的標籤?此外,我會喜歡搜索ID,而不是 類,因爲我可以一次性獲取所有數據。我會如何做到這一點從我上面?謝謝。如果我正確使用它,我也不確定glob函數嗎?
編輯
這裏是我編輯的代碼,沒有錯誤但回到我不知道,如果它返回所有的E系列機型?
import string, urllib2, urllib, csv, urlparse from bs4 import
BeautifulSoup
##-page which shows results after selecting one option-##
url = 'http://www.asusparts.eu/partfinder/Asus/All In One/E Series/ET10B'
base_url = 'http://www.asusparts.eu/' + url
print base_url
##-page to show all selections for the E-series-##
selected_list = urllib.quote(base_url + '/Asus/All In One/E Series/ET10B')
print urllib.quote(base_url + '/Asus/All In One/E Series/ET10B')
#selected_list = 'http://www.asusparts.eu/partfinder/Asus/All In One/E Series/ET10B'
##-
page = urllib2.urlopen('http://www.asusparts.eu/partfinder/Asus/All%20In%20One/E%20Series')
soup = BeautifulSoup(page)
print soup
##-identify the id of select list which contains the E-series-##
select = soup.find('select', id="myselectListModel")
option_tags = select.findAll('option')
print option_tags
##-omit first item in list as isn't part of the option-##
option_tags = option_tags[1:]
print option_tags
for option in option_tags:
url + option['redirectvalue']
print " " + url + option['redirectvalue']
什麼是你放在那裏的for循環?您需要修正縮進,顯示屬於循環中的內容以及不包含的內容。 –
glob()只適用於本地文件,它不適用於URL。您需要一些其他方法來查明該網站上存在哪些網頁,這可能涉及解析。 –
請看看我的編輯代碼 – ash