python - BeautifulSoup:无法获取文本

标签 python html web-scraping beautifulsoup

我试图从 eBay.com 获取商品的价格,但由于某种原因我无法获取。

price_BeautifulSoup = bs.find("span", {"id": "prcIsum"}).text
# What the console prints: "<class 'bs4.BeautifulSoup'>"

我尝试过的:

  • 我尝试过用谷歌搜索“class 'bs4.BeautifulSoup'”
  • price_BeautifulSoup = bs.find('span', attrs={"id": "prcIsum"}).get_text().strip()

enter image description here

整个代码:https://pastebin.com/Vb5gd7RL

提前致谢。说真的。

最佳答案

您打印了错误的变量:

print("price_BeautifulSoup", ": ", BeautifulSoup,"\n")

你想要:

print("price_BeautifulSoup", ": ", price_BeautifulSoup ,"\n")

如果您想要单独的价格,您可以提取内容属性

bs.select_one('#prcIsum')['content']

如果您想匹配 selenium 列表输出,然后按空格分割:

print("price_selenium", ": ", price_selenium)
print("price_BeautifulSoup", ": ", price_BeautifulSoup.split(' ') ,"\n")

关于python - BeautifulSoup:无法获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58161241/

相关文章:

python - matplotlib 中的颜色

python - 将 n-gram 合并或反转为单个字符串

javascript - 使用 jquery 创建了一个简单的 BREAKOUT 游戏,尝试转换回纯 JavaScript

javascript - 在 X 个结果后包装数据的 CSS 元素

javascript - 如何让我的整个弹出窗口背景颜色改变

java - 使用 Jsoup 获取亚马逊数据价格表

python - WSGI - 将内容类型设置为 JSON

python - 为什么将字符串的总和转换为 float

python - 从 javascript 生成的链接中抓取下载网址

excel - 使用 VBA 和 selenium 抓取时从部分 id 中获取全部值(value)