Skip to main content
 首页 » 编程设计

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

2024年10月24日8myhome

我用的是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")) 
[] 

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