如何刮取相当于:在浏览器中突出显示整个页面(即不是页面源),在记事本中复制/粘贴(即没有超链接,只显示文本)的
class TextOnlySpider(scrapy.Spider):
name = "onepage"
allowed_domains = ["en.wikipedia.org"]
start_urls = ['https://en.wikipedia.org/wiki/Congee']
def start_requests(self):
urls = [
'https://en.wikipedia.org/wiki/Congee'
]
for url in urls:
yield scrapy.Request(url=url, callback=self.parse)
def parse(self, response):
# Below line gives HTML/javascript, etc.
# I only want TEXT equaivalent. i.e Same text content that one gets, by copying in browser and pasting in Notepad/vim
bodyText = '\n'.join(response.xpath('//text()').extract())
yield{
'text': bodyText, #TODO only get TEXT equivalent of rendered page (text seen by human eyes)
'title': response.url, #TODO change to title
'id':response.url,
}我要的是人类阅读的文本,而不是这个答案中的页面源:
原因:
我将获得文本表示,并在elasticsearch中对其进行页面url和索引,从而使其成为一个站点搜索解决方案。我不想在索引时使用乱七八糟的html/js代码。
发布于 2020-05-05 03:50:17
模块html2text可以在删除标记的同时将html转换为纯文本:
import html2text
converter = html2text.HTML2Text()
bodyText = converter.handle(response.text)如果您还想获得呈现的文本,您将需要像Splash这样的无头浏览器来首先呈现页面。
https://stackoverflow.com/questions/61605129
复制相似问题