我不断收到错误:
的语法无效1.add_xpath('tagLine', '//p[@class="tagline"]/text()')
而且我似乎无法弄清楚为什么它会给我这个错误,因为据我所知它与所有其他 1.add_xpath() 方法的语法相同。我的另一个问题是如何请求其他页面。基本上我正在浏览一个大页面并让它浏览页面上的每个链接,然后一旦页面完成,我希望它转到下一个大页面的下一个(按钮),但我不知道怎么做。
def parse(self, response):
hxs = HtmlXPathSelector(response)
for url in hxs.select('//a[@class="title"]/@href').extract():
yield Request(url, callback=self.description_page)
for url_2 in hxs.select('//a[@class="POINTER"]/@href').extract():
yield Request(url_2, callback=self.description_page)
def description_page(self, response):
l = XPathItemLoader(item=TvspiderItem(), response=response)
l.add_xpath('title', '//div[@class="m show_head"]/h1/text()')
1.add_xpath('tagLine', '//p[@class="tagline"]/text()')
1.add_xpath('description', '//div[@class="description"]/span')
1.add_xpath('rating', '//div[@class="score"]/text()')
1.add_xpath('imageSrc', '//div[@class="image_bg"]/img/@src')
return l.load_item()
对此的任何帮助将不胜感激。对于 python 和 scrapy 来说,我还是一个菜鸟。
最佳答案
def description_page(self, response):
l = XPathItemLoader(item=TvspiderItem(), response=response)
l.add_xpath('title', '//div[@class="m show_head"]/h1/text()')
1.add_xpath('tagLine', '//p[@class="tagline"]/text()')
1.add_xpath('description', '//div[@class="description"]/span')
1.add_xpath('rating', '//div[@class="score"]/text()')
1.add_xpath('imageSrc', '//div[@class="image_bg"]/img/@src')
return l.load_item()
您使用数字 1
而不是变量名称 l
。
关于python - 如何用scrapy解析多个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8938952/