如何获取url列表并在scrapy python中用于web数据提取
我正在使用scrapy python创建web scraper。这是我的密码如何获取url列表并在scrapy python中用于web数据提取,python,python-3.x,web-scraping,woocommerce,scrapy,Python,Python 3.x,Web Scraping,Woocommerce,Scrapy,我正在使用scrapy python创建web scraper。这是我的密码 import scrapy class BlogSpider(scrapy.Spider): name = 'blogspider' start_urls = [ 'https://perfumehut.com.pk/shop/', ] def parse(self, response): yield { 'produ
import scrapy
class BlogSpider(scrapy.Spider):
name = 'blogspider'
start_urls = [
'https://perfumehut.com.pk/shop/',
]
def parse(self, response):
yield {
'product_link': response.css('a.product-image-link::attr("href")').get(),
'product_title': response.css('h3.product-title>a::text').get(),
'product_price': response.css('span.price > span > bdi::text').get(),
}
next_page = response.css('ul.page-numbers>li>a.next.page-numbers::attr("href")').get()
if next_page is not None:
print()
print(next_page)
print()
yield scrapy.Request(next_page)
def parse(self, response):
yield {
'title': response.css('h1::text').get(),
'batt': response.css('td.woocommerce-product-attributes-item__value p::text')[3].get(),
'brand': response.css('div.woodmart-product-brand img::attr(alt)').get(),
'brandimg': response.css('div.woodmart-product-brand img::attr(src)').get(),
'price': response.css('p.price').xpath('./span/bdi/text()').get(),
'r-price': response.css('p.price').xpath('./del/span/bdi/text()').get(),
's-sale': response.css('p.price').xpath('./ins/span/bdi/text()').get(),
'breadcrumbs': response.css('nav.woocommerce-breadcrumb a::text').getall(),
'tags': response.css('span.tagged_as a::text').getall(),
'attributes': response.css('td.woocommerce-product-attributes-item__value p::text').getall(),
'img': response.css('figure.woocommerce-product-gallery__image a::attr("href")').getall(),
'description': response.css('div.woocommerce-product-details__short-description p::text').get(),
'description1': response.css('#tab-description > div > div > p::text').getall(),
'description2': response.css('#tab-description > div > div > div > div > div > div > div > div > p::text').getall()
}
这是一个商业网站。
共有57页,每页12种产品。
估计总共有684种产品
但是我的代码什么也不返回
抓取url时我做错了什么?要提取所有页面信息,需要提取下一个页面url,然后解析url 这里有一个简单的例子,我认为这有助于你解决问题
import scrapy
class BlogSpider(scrapy.Spider):
name = 'blogspider'
start_urls = [
'https://perfumehut.com.pk/shop/',
]
def parse(self, response):
yield {
'product_link': response.css('a.product-image-link::attr("href")').get(),
'product_title': response.css('h3.product-title>a::text').get(),
'product_price': response.css('span.price > span > bdi::text').get(),
}
next_page = response.css('ul.page-numbers>li>a.next.page-numbers::attr("href")').get()
if next_page is not None:
print()
print(next_page)
print()
yield scrapy.Request(next_page)
好的,这个应该可以:
class BlogSpider(scrapy.Spider):
name = 'blogspider'
start_urls = [
'https://perfumehut.com.pk/shop/',
]
def parse(self, response):
for item in response.css(".product-grid-item"):
yield {
'product_link': item.css('a.product-image-link::attr("href")').get(),
'product_title': item.css('h3.product-title > a::text').get(),
'product_price': item.css('span.price > span > bdi::text').get(),
}
next_page = response.css('a.next:contains(→)::attr("href")').get()
if next_page:
yield scrapy.Request(next_page)
先生,我的完整代码是什么,实际上我需要从每个url提取数据意味着单个产品页面您可以从所有页面提取每个产品url,然后遍历每个产品url以收集所需的数据。先生,我已经用您的代码更新了有问题的代码,但它在csv中不返回任何内容。你能告诉我哪里出错了吗?你应该删除第二个解析方法。这不是你提取数据的方式。先生,我用scrapy运行你的代码,它只提供每页第一个产品的数据。每页有12个产品,它只返回每页第一项的数据。虽然这段代码可能会回答这个问题,但它如何或为什么解决这个问题将真正有助于提高您的文章质量。请记住,你是在将来回答读者的问题,而不仅仅是现在提问的人。请在回答中添加解释,并说明适用的限制和假设。