【发布时间】:2019-12-21 02:35:11
【问题描述】:
我有一个蜘蛛,我想抓取一篇我感兴趣的文章,然后将标题和内容存储在字典中。但是,当我抓取正文时,它会返回 html 代码,我想将其转换为文本(包括文章中的所有 h1 和 href),但是当我使用 .getall() 时,它会返回一个空列表。我如何将这一切都变成文本,并且仍然保留文章中的所有内容。
在我尝试过的scrapy shell 中,它返回了一个包含所有html 代码的大列表。
response.css("div.rich-text-content").getall()
下面是我为了完成这项任务而创建的初始蜘蛛...
class ArticleSpider(scrapy.Spider):
name = "article"
def start_requests(self):
urls = [
"https://www.codehousegroup.com/insight-and-inspiration/tech-stream/what-is-machine-learning"
]
for url in urls:
yield scrapy.Request(url=url, callback=self.parse)
def parse(self, response):
for quote in response.css("div.article-page"):
yield {
'heading': quote.css("h1::text").get(),
'text': quote.css("p.rectangle-decoration::text").get(),
'body': quote.css("div.rich-text-content rich-text-content::text").getall(),
}
预期结果是一个字符串,其中包含当前在我的字典正文项中的所有内容,只是没有标签。
【问题讨论】:
-
有很好用的
html2text模块
标签: python web-scraping scrapy