繁体   English   中英

找不到元素 Beautifulsoup 网络抓取 londonstockexchange

[英]Can't find element Beautifulsoup web-scraping londonstockexchange

我正在尝试抓取以下网站:

www.londonstockexchange.com/news-article/THRG/net-asset-value-s/15242427

基本上我只想保存文本,即以下内容:

“贝莱德 Throgmorton Trust PLC 于 2021 年 12 月 7 日收盘时的未经审计净资产值为:938.74p 资本仅 947.82p 包括本年度收入”

我尝试使用以下代码,但是,我似乎无法解析元素。 任何想法为什么?

url = "https://www.londonstockexchange.com/news-article/THRG/net-asset-value-s/15242427"
page = requests.get(url) # Requests website
soup = BeautifulSoup(page.content, 'html.parser')
table = soup.find_all('div', attrs={'class':'news-body-content'})
table

然而,尝试了各种方式,没有运气。 希望有人可以提供帮助。

这是因为特定文本是通过 JavaScript 动态添加到页面的,而在您的代码中,您只是向服务器发出请求,然后获取返回的数据(即页面源)。

It is the client (ie your browser) that runs the JavaScript code, but BeautifulSoup on its own does not have that capability, so you will need to use a different library to render the JavaScript content first, and then BeautifulSoup to parse it.

我同意这里的所有其他海报; 为了正确抓取这个,您应该使用执行 JavaScript 的不同库。

但是,如果您是一个贪吃疼痛的人并且只能使用 BeautifulSoup,那么您正在寻找的信息是可以访问的。

页面底部有一个脚本标签,您可以使用soup.find(id='ng-lseg-state')来访问它。 这将是一个混乱的字符串,但信息在那里:

\nNET ASSET VALUE\ n\ nBLACKROCK THROGMORTON TRUST PLC\ n5493003B7ETS1JEDPF59\ n\ nThe unaudited
                                net asset values
                                for BlackRock Throgmorton Trust PLC at close of \
                                nbusiness on 7 December 2021 were: \n\ n938 .74 p Capital only\ n947 .82 p Including current year income\ n

超级丑陋,我知道,但它就在那里。 可能应该只做@hsac 并使用不同的库。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM