所以我对刮痧和学刮痧完全是新手
https://www.killertools.com/Dent-Removal-Aluminum-Steel_c_11.html 首先,如果有超过一页的产品可供浏览,我想从两个页面中删除第一类中所有产品的项目名称
这就是我所得到的,它是有效的:
import scrapy
class QuotesSpider(scrapy.Spider):
name = 'killertools'
start_urls = ['https://www.killertools.com/Dent-Removal-Aluminum-Steel_c_11.html',
]
def parse(self, response):
for item in response.css('div.name'):
yield {'Name': item.xpath('a/text()').get()}
next_page = response.css('div.paging a:nth-child(4)::attr("href")').get()
if next_page is not None:
yield response.follow(next_page, self.parse)
但我想进入每个产品链接,并提取项目描述,并将其作为描述放入词汇表中。我该怎么做呢
我试过这样的方法:
import scrapy
class QuotesSpider(scrapy.Spider):
name = 'killertools'
start_urls = ['https://www.killertools.com/Dent-Removal-Aluminum-Steel_c_11.html',
]
def parse(self, response):
for item in response.css('div.name'):
yield {'Name': item.xpath('a/text()').get()}
detail_page = response.css('div.name a::attr("href")').get()
if detail_page is not None:
yield response.follow(detail_page)
for detail in response.css('div.item'):
yield {'Description': detail.xpath('p/strong/text').get()}
next_page = response.css('div.paging a:nth-child(4)::attr("href")').get()
if next_page is not None:
yield response.follow(next_page, self.parse)
但它做了一些奇怪的事情,我真的无法以我的水平来思考
建议0
试着把你的头绕在XPATH上
建议1
在页面底部,靠近分页链接,您将看到一个“全部查看”。这会将
?viewall=1
添加到原始URL。对于您提供的URL,所有21项都放在一个上。似乎您不再需要担心分页了建议2
为了获取产品描述,可以设想两步流程:
response.xpath('//div[contains(@class, "product-item")]/div[@class="name"]/a/@href').getall()
应该会让你离得很近。
检查URL。您可能需要在基本url前面加上前缀
urllib
是你的朋友李>在产品页面上,您可以找到结构良好的描述:
response.xpath('//div[@itemprop="description"]/ul/li/text()').getall()
会让你在要点后面排好所有的线李>
建议3
仁慈点!不要用不必要的请求敲打他们的网站。测试时,自定义设置应包括
'HTTPCACHE_ENABLED': True
。请查看HTTPCACHE了解详细信息祝你好运,玩得开心
相关问题 更多 >
编程相关推荐