我试图从一个网页收集数据,其中有一堆选择列表,我需要获取 数据来自。这是页码:http://www.asusparts.eu/partfinder/Asus/All在一个/E系列中/
到目前为止我得到的是:
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系列的所有模型?在
^{pr2}$
首先,我想指出您发布的代码中存在的几个问题。首先,
glob
模块通常不用于发出HTTP请求。它对于迭代指定路径上的文件子集非常有用,您可以阅读有关它的更多信息in its docs。在第二个问题是:
您有一个缩进错误,因为后面没有缩进的代码。并阻止代码的其余部分被执行。在
另一个问题是,您正在为变量使用一些python的“保留”名称。千万不要用
all
或file
等词来表示变量名。在最后,当您循环
^{pr2}$option_tags
时:open
语句将尝试打开路径为url + option['value']
的本地文件。这可能会引发一个错误,因为我怀疑您在该位置是否有一个文件。另外,你应该知道你没有对这个打开的文件做任何事情。在好吧,评论到此为止。我已经看了华硕的网页,我想我有一个想法,你想完成什么。据我所知,你想在华硕页面上为每台电脑型号刮出一份零件清单(图片、文字、价格等)。每个模型在一个惟一的URL(例如:http://www.asusparts.eu/partfinder/Asus/Desktop/B%20Series/BM2220)上都有它的部件列表。这意味着您需要能够为每个模型创建这个惟一的URL。更复杂的是,每个零件类别都是动态加载的,因此,例如,“冷却”部分的零件在单击“冷却”链接之前不会加载。这意味着我们有一个由两部分组成的问题:1)获取所有有效的(品牌、类型、系列、型号)组合;2)找出如何加载给定型号的所有部件。在
我有点无聊,决定写一个简单的程序来处理大部分的繁重工作。这不是最优雅的东西,但它会完成任务的。步骤1)在} 就会运行,它会对格式化的
get_model_information()
中完成。步骤2)在parse_models()
中得到了处理,但不太明显。看看华硕网站,每当你点击一个部件小节,JavaScript函数^{PRODUCT_URL
(见下文)进行ajax调用。响应是一些JSON信息,用于填充您单击的部分。在最后,有一点需要注意。我已经放弃了
urllib2
,转而使用requests
库。我个人认为它提供了更多的功能和更好的语义,但是你可以使用你想要的任何东西。在相关问题 更多 >
编程相关推荐