如何提取文本在一个扩展更多按钮使用scrapy?

2024-06-01 04:31:46 发布

您现在位置:Python中文网/ 问答频道 /正文

在URL中: https://teslamotorsclub.com/tmc/threads/tesla-tsla-the-investment-world-the-2019-investors-roundtable.139047/page-2619

邮编:52365

在我得到文本之前,我需要点击“扩展更多”,我怎样才能得到里面的文本?有没有一种方法可以在运行spider脚本时触发expand more来显示整体?在

到目前为止我所做的是

在信息.xpath(“//div[@class='messageContent']”)。extract_first().replace('\n','')

但我还是不能得到全文


Tags: thehttps文本comurlworldpagethreads
2条回答

您可能会在结尾看到“单击以展开”文本,但仍然会得到整个引用。您需要的是避免提取“单击以展开”文本。在

例如:

>>> response.xpath('//li[contains(@class, "message")][.//a/text()[.="#52365"]]//*[re:test(@class, "\\bquote\\b")]//text()').getall()
['CCS for model 3 coming', '\nWhile article references Europe, the North American theater will be getting a CCS adapter soon.', '\nSee article for', '\n', '\n', 'Tesla launches $190 CCS adapter for new Model S and Model X, offers retrofits for older vehicles', '\n', '\nMartian High Command', '\n', '\nPS: Text from article.', '\n', '\nUpdate: A Tesla spokesperson told us that they will make sure owners in North America will have access to all “compelling networks”, but they have nothing to announce now.']

正如有人在评论中指出的,你不需要点击任何东西。如果在浏览器中打开“文档检查器”,则可以看到所有文本都在其中。在

您可以使用简单的css选择器和for循环检索所有邮件:

for post in sel.css('.messageList>li'): 
    text = ''.join(post.css('blockquote.messageText ::text').extract()) 
    print(text) 
    print('   ')

相关问题 更多 >