python - 如何使用 Beautiful soup 和 urllib.request 获取隐藏值

标签 python python-3.x

我在 Windows10X64 上使用 Python3(安装了 Ananconda)。 我试图使用 urllibBeautifulSoup 获取“span”元素的值,在 Chrome 中,它显示 8000,但我的代码结果总是给出 0 个字母。

有人可以建议如何获取 Chrome 网络浏览器中显示的真实数字吗?

这是我的代码。

from bs4 import BeautifulSoup
from urllib.request import Request, urlopen
url ='https://www.futbin.com/18/squad/100133002/sbc'
req = Request(url,headers={'User-Agent': 'Mozilla/5.0'})
page_html = urlopen(req).read()
page_soup = BeautifulSoup(page_html,'html.parser')

page_soup.findAll("div", {"class": "ps4-price"})

结果如下。

[<div class="ps4-price">
 <img class="price-platform-img" src="https://cdn.futbin.com/design/img/logos/full_small/ps_blue.png"/>
 <div class="price-row-text" id="squad-price-ps3"><span class="psprice2">0</span></div>
 </div>]

在“span”标签中,它应该显示与 Chrome/Firefox 中所示相同的值(例如 8000,9000)

最佳答案

您可以通过这种方法:

>>> url = 'https://www.futbin.com/18/squad/100133002/sbc'
>>> req = Request(url,headers={'User-Agent': 'Mozilla/5.0'})
>>> webpage = urlopen(req).read()
>>> soup = BeautifulSoup(webpage, "html.parser")
>>> required = soup.find_all("div", {"class":"pcdisplay-ps-price"})
>>> x = []
>>> for i in required:
...     x.append(i.get_text())
>>> for i in x:
...     print(i)
...

950
550
750
600
1,300
900
850
550
600
600
600

关于python - 如何使用 Beautiful soup 和 urllib.request 获取隐藏值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48636331/

相关文章:

python - 如何根据现有列 Pandas 的多个条件创建新列

python - 监控文件变化 - Asyncio 和 Flask

python - 如何在 Windows 上 pip 或 easy_install tkinter

python - 将行添加到 csv 文件而不创建中间副本

python - 打印没有字典名称的字典键?怎么/为什么?

python - 在 flask 中运行破折号应用程序

python - 在 Flask 部署中 Web 服务器有什么用?

python - DRF : ModelSerializer for nested JSON data

python - Hive查询使用Python返回空白,但直接查询时没有问题

python - 基于类的 View VS 基于函数的 View