【问题标题】:Python BeautifulSoup: equivalent to a browser node.innerTextPython BeautifulSoup:相当于浏览器的nod​​e.innerText
【发布时间】:2021-02-17 04:19:45
【问题描述】:

我一直在尝试使用 pythons beautifulsoup 库从抓取的站点中提取一些数据。我发现搜索节点并在其上使用 .text 来获取其内容确实有效,但不知何故弄乱了格式,并且字符串最终没有像在网页上那样被格式化。我发现有一种在浏览器中使用 .innerText 的方法,它返回一些 html 文本的字符串,就好像用户会复制粘贴它一样。在 python 中是否有等价物,还是我真的需要在浏览器中运行它?

【问题讨论】:

  • 你能分享 URL 和预期的输出吗?还有一个代码你到目前为止尝试过什么......

标签: javascript python html beautifulsoup screen-scraping


【解决方案1】:

你可以用beautifulsoup做到这一点,你只需在你的汤对象后面使用这个.text就可以了

from bs4 import BeautifulSoup
import requests

r = requests.get("https://YourUrl.com")
soup = BeautifulSoup(r.text, "html.parser")
result = soup.find_all('body') # this will give you everything in body, 
# if you want anything else, you can specify by adding a dictionary into the parameters

【讨论】:

  • 这实际上不起作用,因为某些文本节点包含未在实际浏览器中呈现的空格/换行符/制表符。
猜你喜欢
  • 2011-05-26
  • 1970-01-01
  • 2010-09-07
  • 2010-10-21
  • 2020-09-14
  • 2018-03-22
  • 2020-07-27
  • 2012-09-16
  • 1970-01-01
相关资源
最近更新 更多