用Scrapy1.5.0和Python2.7.14处理URL时出错。你知道吗
class GoodWillOutSpider(Spider):
name = "GoodWillOutSpider"
allowded_domains = ["thegoodwillout.com"]
start_urls = [GoodWillOutURL]
def __init__(self):
logging.critical("GoodWillOut STARTED.")
def parse(self, response):
products = Selector(response).xpath('//div[@id="elasticsearch-results-container"]/ul[@class="product-list clearfix"]')
for product in products:
item = GoodWillOutItem()
item['name'] = product.xpath('//div[@class="name ng-binding"]').extract()[0]
item['link'] = "www.thegoodwillout.com" + product.xpath('//@href').extract()[0]
# item['image'] = "http:" + product.xpath("/div[@class='catalogue-product-cover']/a[@class='catalogue-product-cover-image']/img/@src").extract()[0]
# item['size'] = '**NOT SUPPORTED YET**'
yield item
yield Request(GoodWillOutURL, callback=self.parse, dont_filter=True, priority=16)
这是我的类GoodWillOutSpider,这是我得到的错误:
[scrapy.core.scraper] ERROR: Spider error processing <GET https://www.thegoodwillout.com/footwear> (referer: None)
line 1085, in parse item['name'] = product.xpath('//div[@class="name ng-binding"]').extract()[0] IndexError: list index out of range
我想知道,在未来,我如何不在这里再次询问每个站点的正确xpath
问题
如果您的scraper无法访问您可以使用浏览器开发工具看到的数据,那么它将无法看到与您的浏览器相同的数据。你知道吗
这可能意味着两件事之一:
通用解决方案
解决这两个问题最直接的方法是使用实际的浏览器。你知道吗
有许多无头浏览器可用,您可以根据自己的需要选择最好的。
对于scrapy,scrapy-splash可能是最简单的选择。你知道吗
更专业的解决方案
有时,您可以找出这种不同行为的原因,然后更改代码。
这通常是更有效的解决方案,但可能需要您做更多的工作。你知道吗
例如,如果您的scraper被重定向,那么您可能只需要使用不同的用户代理字符串,传递一些附加的头,或者减慢您的请求。你知道吗
如果内容是由javascript生成的,那么您可以查看页面源代码(
response.text
或在浏览器中查看源代码),并找出发生了什么。你知道吗之后,有两种可能:
您需要首先检查列表在提取后是否有任何值
相关问题 更多 >
编程相关推荐