我试图从gsmarena页面下载数据:“http://www.gsmarena.com/htc_one_me-7275.php”。在
但是,数据是以表和表行的形式分类的。 数据格式如下:
table header > td[@class='ttl'] > td[@class='nfo']
编辑代码:感谢stackexchange社区成员的帮助,我将代码重新格式化为: 项目.py文件:
^{pr2}$蜘蛛文件:
from scrapy.selector import Selector
from scrapy import Spider
from gsmarena_data.items import gsmArenaDataItem
class testSpider(Spider):
name = "mobile_test"
allowed_domains = ["gsmarena.com"]
start_urls = ('http://www.gsmarena.com/htc_one_me-7275.php',)
def parse(self, response):
# extract whatever stuffs you want and yield items here
hxs = Selector(response)
phone = gsmArenaDataItem()
tableRows = hxs.css("div#specs-list table")
for tableRows in tableRows:
phone['phoneName'] = tableRows.xpath(".//th/text()").extract()[0]
for ttl in tableRows.xpath(".//td[@class='ttl']"):
ttl_value = " ".join(ttl.xpath(".//text()").extract())
nfo_value = " ".join(ttl.xpath("following-sibling::td[@class='nfo']//text()").extract())
colonSign = ": "
commaSign = ", "
seq = [ttl_value, colonSign, nfo_value, commaSign]
phone['phoneDetails'] = "".join(seq)
yield phone
但是,只要我尝试使用以下命令加载页面,我就会被禁止:
"http://www.gsmarena.com/htc_one_me-7275.php"
我甚至尝试过使用下载延迟=3英寸设置.py. 在
请建议我该怎么做。在
我还面临着同样的问题:在几个请求中被禁止,使用scrapy-proxies和使用{a2}更改代理有很大帮助,但没有完全解决问题。在
你可以在gsmarenacrawler找到我的代码
其思想是迭代“spec list”中的所有
table
元素,获取块名称的th
元素,获得所有带有class="ttl"
的td
元素以及与class="nfo"
对应的td
同级。在来自shell的演示:
相关问题 更多 >
编程相关推荐