我在努力为漫画书搜集定价信息。我最终得到的是一个Spider,它通过顶部css选择器的所有实例,然后返回只包含我所追求的定价信息的选择器的第一个实例的期望值。
我的最终目标是能够创建一个管道,为实际的清单提供一个带有标题、sku、价格和url的SQLite db。这是我的代码:
class XmenscrapeSpider(scrapy.Spider):
name = 'finalscrape'
allowed_domains = ['mycomicshop.com']
start_urls = ['https://www.mycomicshop.com/search?TID=222421']
def parse(self, response):
for item in response.css('td.highlighted'):
yield {
'title' : response.xpath('.//meta[@itemprop="sku"]/@content').get()
}
next_page = response.css('li.next a::attr(href)').extract()[1]
if next_page is not None:
yield resonse.follow(next_page, callback- self.parse)我的输出如下:
{'title': '100 Bullets (1999 DC Vertigo) 1 CGC 9.8'}
2022-01-24 13:53:04 [scrapy.core.scraper] DEBUG: Scraped from <200
https://www.mycomicshop.com/search?TID=222421>
{'title': '100 Bullets (1999 DC Vertigo) 1 CGC 9.8'}
2022-01-24 13:53:04 [scrapy.core.scraper] DEBUG: Scraped from <200
https://www.mycomicshop.com/search?TID=222421>
{'title': '100 Bullets (1999 DC Vertigo) 1 CGC 9.8'}
2022-01-24 13:53:04 [scrapy.core.scraper] DEBUG: Scraped from <200
https://www.mycomicshop.com/search?TID=222421>
{'title': '100 Bullets (1999 DC Vertigo) 1 CGC 9.8'}
2022-01-24 13:53:04 [scrapy.core.scraper] DEBUG: Scraped from <200
https://www.mycomicshop.com/search?TID=222421>
{'title': '100 Bullets (1999 DC Vertigo) 1 CGC 9.8'}如果您查看我试图抓取的URL,您可以看到,尽管爬行器在页面上迭代它的五个实例,但我只从第一个标记中获得了所需的值。我有一种感觉,这是一个简单的解决方案,但我将在这里结束。对于什么可能是一个简单的解决办法,有什么想法吗?
发布于 2022-01-25 14:18:25
您需要对item使用相对xpath。
import scrapy
class XmenscrapeSpider(scrapy.Spider):
name = 'finalscrape'
allowed_domains = ['mycomicshop.com']
start_urls = ['https://www.mycomicshop.com/search?TID=222421']
def parse(self, response):
for item in response.css('td.highlighted'):
yield {
# 'title': response.xpath('.//meta[@itemprop="sku"]/@content').get()
'title': item.xpath('.//meta[@itemprop="name"]/@content').get()
}
next_page = response.css('li.next a::attr(href)').get()
if next_page:
yield response.follow(next_page, callback=self.parse)注意:您只循环高亮显示的项目,并且由于下一页没有任何内容,您将无法从中得到任何信息。
https://stackoverflow.com/questions/70839133
复制相似问题