为什么当我抓取这段代码试图得到标题,作者和长度
import scrapy
HTTPCACHE_ENABLED = False
class AudibleSpider(scrapy.Spider):
name = "audible"
allowed_domains = ["www.audible.com"]
start_urls = ["https://www.audible.com/"]
def parse(self, response):
product_container=response.xpath('//div[@class="adbl-impression-container "]/div/span/ul/li')
for product in product_container:
book_title=product.xpath('./div/div[1]/div/div[2]/div/div/span/ul/li[1]/h3/a/text()').get()
book_author=product.xpath('./div/div[1]/div/div[2]/div/div/span/ul/li[3]/span/a/text()').getall()
book_len=product.xpath('./div/div[1]/div/div[2]/div/div/span/ul/li[5]/span/text()').get()
yield{
'auther':book_author,
'lenghth':book_len,
'title':book_title,
}
它给我:
2023-02-04 22:50:23 [scrapy.utils.log] INFO: Scrapy 2.8.0 started (bot: spider_new)
2023-02-04 22:50:23 [scrapy.utils.log] INFO: Versions: lxml 4.9.2.0, libxml2 2.9.12, cssselect 1.2.0, parsel 1.7.0, w3lib 2.1.1, Twisted 22.10.0, Python 3.11.1 (tags/v3.11.1:a7a450f, Dec 6 2022, 19:58:39) [MSC v.1934 64 bit (AMD64)], pyOpenSSL 23.0.0 (OpenSSL 3.0.7 1 Nov 2022), cryptography 39.0.0, Platform Windows-10-10.0.19044-SP0
2023-02-04 22:50:23 [scrapy.crawler] INFO: Overridden settings:
{'BOT_NAME': 'spider_new',
'FEED_EXPORT_ENCODING': 'utf-8',
'NEWSPIDER_MODULE': 'spider_new.spiders',
'REQUEST_FINGERPRINTER_IMPLEMENTATION': '2.7',
'ROBOTSTXT_OBEY': True,
'SPIDER_MODULES': ['spider_new.spiders'],
'TWISTED_REACTOR': 'twisted.internet.asyncioreactor.AsyncioSelectorReactor'}
2023-02-04 22:50:23 [asyncio] DEBUG: Using selector: SelectSelector
2023-02-04 22:50:23 [scrapy.utils.log] DEBUG: Using reactor: twisted.internet.asyncioreactor.AsyncioSelectorReactor
2023-02-04 22:50:23 [scrapy.utils.log] DEBUG: Using asyncio event loop: asyncio.windows_events._WindowsSelectorEventLoop
2023-02-04 22:50:23 [scrapy.extensions.telnet] INFO: Telnet Password: c2d2ed1e9abf3b2e
2023-02-04 22:50:23 [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.corestats.CoreStats',
'scrapy.extensions.telnet.TelnetConsole',
'scrapy.extensions.logstats.LogStats']
2023-02-04 22:50:24 [scrapy.middleware] INFO: Enabled downloader middlewares:
['scrapy.downloadermiddlewares.robotstxt.RobotsTxtMiddleware',
'scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',
'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',
'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',
'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware',
'scrapy.downloadermiddlewares.retry.RetryMiddleware',
'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware',
'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware',
'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',
'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',
'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware',
'scrapy.downloadermiddlewares.stats.DownloaderStats']
2023-02-04 22:50:24 [scrapy.middleware] INFO: Enabled spider middlewares:
['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
'scrapy.spidermiddlewares.referer.RefererMiddleware',
'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
'scrapy.spidermiddlewares.depth.DepthMiddleware']
2023-02-04 22:50:24 [scrapy.middleware] INFO: Enabled item pipelines:
[]
2023-02-04 22:50:24 [scrapy.core.engine] INFO: Spider opened
2023-02-04 22:50:24 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2023-02-04 22:50:24 [scrapy.extensions.telnet] INFO: Telnet console listening on 127.0.0.1:6024
2023-02-04 22:50:25 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.audible.com/robots.txt> (referer: None)
2023-02-04 22:50:26 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.audible.com/> (referer: None)
2023-02-04 22:50:26 [scrapy.core.engine] INFO: Closing spider (finished)
2023-02-04 22:50:26 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 440,
'downloader/request_count': 2,
'downloader/request_method_count/GET': 2,
'downloader/response_bytes': 103231,
'downloader/response_count': 2,
'downloader/response_status_count/200': 2,
'elapsed_time_seconds': 2.018748,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2023, 2, 4, 20, 50, 26, 805751),
'httpcompression/response_bytes': 1471510,
'httpcompression/response_count': 1,
'log_count/DEBUG': 5,
'log_count/INFO': 10,
'response_received_count': 2,
'robotstxt/request_count': 1,
'robotstxt/response_count': 1,
'robotstxt/response_status_count/200': 1,
'scheduler/dequeued': 1,
'scheduler/dequeued/memory': 1,
'scheduler/enqueued': 1,
'scheduler/enqueued/memory': 1,
'start_time': datetime.datetime(2023, 2, 4, 20, 50, 24, 787003)}
2023-02-04 22:50:26 [scrapy.core.engine] INFO: Spider closed (finished)
它没有给我一个错误,但这不是我想要的。
我使用shell命令测试了xpath,结果是正确的。
像这样的事情发生在我以前,但当我保存文件,它的工作,这一次没有任何工作。
为什么会发生这种情况以及如何解决
1条答案
按热度按时间mdfafbf11#
正如在注解中提到的,选择器并没有指向要提取的元素。
使用元素属性和CSS选择器要容易得多,因为浏览器生成的路径经常是错误的。
例如,下面是我如何能够得到同样的信息。
输出