Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python BeautifulSoup.find_Python_Python 3.x_Beautifulsoup_Urllib - Fatal编程技术网

Python BeautifulSoup.find

Python BeautifulSoup.find,python,python-3.x,beautifulsoup,urllib,Python,Python 3.x,Beautifulsoup,Urllib,我想使用urllib和BeautifulSoup从网站上获取一些特定数据。 我正在试图获取文本“190.0 kg”。正如您在我的代码中看到的,我已经尝试使用attrs={'class':'col-md-7'} 但这会返回错误的结果。有没有办法指定我希望它返回之间的文本 从urllib.request导入urlopen 从bs4导入BeautifulSoup #指定url 引述https://styrkeloft.no/live.styrkeloft.no/v2/?test-斯特夫内 #查询网站

我想使用urllib和BeautifulSoup从网站上获取一些特定数据。 我正在试图获取文本“190.0 kg”。正如您在我的代码中看到的,我已经尝试使用
attrs={'class':'col-md-7'}
但这会返回错误的结果。有没有办法指定我希望它返回
之间的文本

从urllib.request导入urlopen
从bs4导入BeautifulSoup
#指定url
引述https://styrkeloft.no/live.styrkeloft.no/v2/?test-斯特夫内
#查询网站并将html返回到变量“page”
页面=urlopen(引用页面)
#使用BeautifulSoup解析html
soup=BeautifulSoup(页面“html.parser”)
#取出of name并获取其值
Weight_box=soup.find('div',attrs={'class':'col-md-7'})
name=name\u box.text.strip()
印刷品(名称)

由于此内容是动态生成的,因此无法使用
请求
模块访问该数据

您可以使用selenium webdriver来完成以下操作:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup

chrome_options = Options()
chrome_options.add_argument("--headless")

chrome_driver = "path_to_chromedriver"

driver = webdriver.Chrome(chrome_options=chrome_options,executable_path=chrome_driver)
driver.get('https://styrkeloft.no/live.styrkeloft.no/v2/?test-stevne')
html = driver.page_source
soup = BeautifulSoup(html, "lxml")
current_lifter = soup.find("div", {"id":"current_lifter"})
value = current_lifter.find_all("div", {'class':'row'})[2].find_all("h3")[0].text
driver.quit()

print(value)

请确保您的计算机中有可执行文件。

您能提供实际的url吗?返回错误结果是什么意思?你能发布你得到的输出吗?取而代之的是“visstream”。在html中搜索“Vis stream”,发现它在col-md-7 visible-xs下。用真实的url更新了帖子。@markusl2检查页面源代码。内容不可用,它是用Javascript动态生成的。您不能简单地使用
请求
模块来解决这个问题。看一看。工作得很有魅力。非常感谢。很乐意帮忙。请考虑将答案标记为正确。
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup

chrome_options = Options()
chrome_options.add_argument("--headless")

chrome_driver = "path_to_chromedriver"

driver = webdriver.Chrome(chrome_options=chrome_options,executable_path=chrome_driver)
driver.get('https://styrkeloft.no/live.styrkeloft.no/v2/?test-stevne')
html = driver.page_source
soup = BeautifulSoup(html, "lxml")
current_lifter = soup.find("div", {"id":"current_lifter"})
value = current_lifter.find_all("div", {'class':'row'})[2].find_all("h3")[0].text
driver.quit()

print(value)