我在 Windows10X64 上使用 Python3(安装了 Ananconda)。 我试图使用 urllib 和 BeautifulSoup 获取“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/