如何通过Python3获取html信息
我正在尝试从url获取数据。部分html选项卡如下所示,我想得到数字“397”,它将像股票指数一样随时更改。我的代码如下所示,当我编译.py文件时,结果是如何通过Python3获取html信息,html,python-3.x,beautifulsoup,tabs,Html,Python 3.x,Beautifulsoup,Tabs,我正在尝试从url获取数据。部分html选项卡如下所示,我想得到数字“397”,它将像股票指数一样随时更改。我的代码如下所示,当我编译.py文件时,结果是没有数字 Html: 我的代码有问题吗?谢谢无需找到两个标签即可获取文本您可以直接获取文本 from bs4 import BeautifulSoup import requests html = '''<div id="p_bar_bottom" class="p_bar" style="display: inline;">
没有数字
Html:
我的代码有问题吗?谢谢无需找到两个标签即可获取文本您可以直接获取文本
from bs4 import BeautifulSoup
import requests
html = '''<div id="p_bar_bottom" class="p_bar" style="display: inline;">
<a name="p_bar_total" class="p_total"> 397 </a>
<a name="p_bar_min" class="p_redirect" style="display: none;">|‹ 1</a>'''
soup = BeautifulSoup(html, 'html.parser')
total = soup.select('.p_total')[0].text
print(total)
谢谢你的帮助。我认为你的方法是可行的,但我刚刚意识到问题不在于如何显示数字。当我打印出“汤”时,“数字”已经像那样丢失了。但其他标签工作正常。这是什么问题造成的?再次感谢!!你能分享这个网址吗?谢谢,它需要登录,但是我可以和你分享相关的html。当我再次查看html时,我发现“number”实际上是一个变量,但我不知道如何从html获取该变量:
var vmdata={vmcount:548}函数refreshBar(objid){var barsize=divchildren[“p_bar_num”].length;divchildren[“p_bar_total”][0].innerHTML=''+vmdata.vmcount+'';obj=divchildren[“p_bar_min”][0];}
对格式感到抱歉。它有字符限制,无法开始换行。上面的代码有三部分:var、函数和html表单。这是javascript,而不是htmlBeautifulSoup
不执行javascript,因此您必须使用无头浏览器或手动解析javascript以获取“vmcount”值。
with requests.session() as s:
url = 'https://www.sth.com'
page = s.get(url)
soup = BeautifulSoup(page.text, 'html.parser')
total_list = soup.find(class_ = 'p_bar')
total_no_list = total_list.find(class_ = 'p_total')
print(total_no_list)
from bs4 import BeautifulSoup
import requests
html = '''<div id="p_bar_bottom" class="p_bar" style="display: inline;">
<a name="p_bar_total" class="p_total"> 397 </a>
<a name="p_bar_min" class="p_redirect" style="display: none;">|‹ 1</a>'''
soup = BeautifulSoup(html, 'html.parser')
total = soup.select('.p_total')[0].text
print(total)
397