想要使用beautifulsoup从标签中获取文本,尝试了所有方法,但无法正常工作

时间:2019-03-03 10:20:41

标签: python beautifulsoup

我想从标签上得到这个“被拒绝”的文字,我尝试了很多事情,但没有任何帮助。

import bs4
import requests
url="example"

agent = {
    "User-Agent": 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36'}
data = requests.get(url, headers=agent)
soup = bs4.BeautifulSoup(data.text, 'html.parser')

# rejects = soup.select("label._1TSOc")
#rejects = soup.find("label._1TSOc")
#rejects = soup.find("label._1TSOc._3Gol_")
rejects  = soup.find("label",{"class":"_1TSOc"})
print(rejects) #checking either getting data or not, but OUTPUT: None    

for i in rejects:
    print(i.text) #not working

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:0)

您尝试过其中一种吗?

rejects  = soup.find("label",{"class":" _1TSOc _3Gol_ "})
print(rejects.text)

rejects  = soup.find("label",{"data-aut-id":"statusLabel"})
print(rejects.text)

答案 1 :(得分:0)

“已拒绝”不是您的data.text,即使在检查代码时也存在。
这意味着它稍后会由某些(java)脚本添加,并且Beautifulsoup将永远无法访问它,因为这确实会执行脚本。
在加载并执行所有脚本之后,您将不得不使用无头浏览器来访问完全加载并运行的页面状态。这个网站上有很多答案!
例如参见Headless Browser for Python (Javascript support REQUIRED!)
您还可以查看activesoup https://pypi.org/project/activesoup/或如何从python驱动chrome。