gpt4 book ai didi

python - 类内容连字符 "-"的 Beautifulsoup 错误?

转载 作者:太空宇宙 更新时间:2023-11-04 03:14:17 24 4
gpt4 key购买 nike

我用的是python 2.7 + BeautifulSoup 4.4.1

e = BeautifulSoup(data)
s1 = e.find("div", class_="one").get_text() # Successful
s2 = e.find("div", class_="two-three").get_text() # ERROR

最佳答案

问题不在于 bs4 或连字符,问题在于如果没有用户代理,您会返回不同的来源,使用下面的请求我们可以得到您想要的:

In [26]: import requests

In [27]: from bs4 import BeautifulSoup

In [28]: r = requests.get("https://play.google.com/store/apps/details?id=com.zing.zalo",
headers={"User-Agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36"})

In [29]: soup = BeautifulSoup(r.content)

In [30]: print(soup.select("span.rating-count"))
[<span aria-label="573,575 ratings" class="rating-count">573,575</span>]

如果我们在没有用户代理的情况下运行它:

In [31]: from bs4 import BeautifulSoup

In [32]: r = requests.get("https://play.google.com/store/apps/details?id=com.zing.zalo")

In [33]: soup = BeautifulSoup(r.content)

In [34]: print(soup.select("span.rating-count"))
[]

如果您打印每个请求的源代码,您会发现它们非常不同。

关于python - 类内容连字符 "-"的 Beautifulsoup 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36832638/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com