我是python编程和网页抓取的新手。我的目标代码是抓取网站中书籍的标题,价格和网址。然而,我无法在我的终端中获得想要的消息,这是“item_scraped_count”,通常已包含在教程终端中,与我的相反。
这是我的代码块
import scrapy
class BookspiderSpider(scrapy.Spider):
name = "bookspider"
allowed_domains = ["books.toscrape.com"]
start_urls = ["https://books.toscrape.com/"]
def parse(self, response):
books = response.css('article.product_prod')
for book in books:
yield{
'title' : book.css("h3 a::text").get(),
'price' : book.css(".product_price .product_color::text").get(),
'url' : book.css("h3 a").attrib['href']
}
next_page = response.css("li.next a::attr(href)").get()
if next_page is not None:
if 'catalogue/' in next_page:
next_page_url = 'https://books.toscrape.com/' + next_page
else:
next_page_url = 'https://books.toscrape.com/catalogue/' + next_page
yield response.follow(next_page_url, callback=self.parse)
字符串
这是终端中的结果:
{'downloader/request_bytes': 15159,
'downloader/request_count': 51,
'downloader/request_method_count/GET': 51,
'downloader/response_bytes': 2556339,
'downloader/response_count': 51,
'downloader/response_status_count/200': 50,
'downloader/response_status_count/404': 1,
'elapsed_time_seconds': 20.302593,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2023, 12, 27, 12, 54, 23, 623889, tzinfo=datetime.timezone.utc),
'log_count/DEBUG': 54,
'log_count/INFO': 10,
'request_depth_max': 49,
'response_received_count': 51,
'robotstxt/request_count': 1,
'robotstxt/response_count': 1,
'robotstxt/response_status_count/404': 1,
'scheduler/dequeued': 50,
'scheduler/dequeued/memory': 50,
'scheduler/enqueued': 50,
'scheduler/enqueued/memory': 50,
'start_time': datetime.datetime(2023, 12, 27, 12, 54, 3, 321296, tzinfo=datetime.timezone.utc)}
2023-12-27 19:54:23 [scrapy.core.engine] INFO: Spider closed (finished)
型
我想提前感谢你帮我解决这个问题
我试图在终端中获取“item_scraped_count”,但它不显示
1条答案
按热度按时间ffx8fchx1#
没有
item_scraped_count
统计信息,因为没有抓取的项目。没有抓取任何项目,因为您的
books
var是空的,因为'article.product_prod'
选择器没有选择任何东西,因为正确的类名是“product_pod”。