我已经在网站上挖掘了一段时间,但我无法找到我的问题的解决方案。我是一个相当新手的网页抓取,并试图简单地从一个网页上提取一些链接使用美丽的汤。
url = "https://www.sofascore.com/pt/futebol/2018-09-18"
page = urlopen(url).read()
soup = BeautifulSoup(page, "lxml")
print(soup)
在最基本的层面上,所有的im尝试做的是访问网站中的特定标签。我可以自己解决剩下的部分,但我正在努力解决的事实是,我正在寻找的标签不在输出中。
例如:使用内置的find(),我可以获取以下div类标记: class="l
_
_
网格js-页面布局“
但是,我实际查找的是嵌入在树中较低级别的标记的内容。
js-event-list-tournament-events
当我在较低级别的标记上执行相同的find操作时,我没有得到任何结果。
使用基于Azure的Jupyter Notebook,我在stackoverflow上尝试了许多类似问题的解决方案,但没有成功。
谢谢!肯尼
发布于 2018-10-07 11:42:13
页面使用JS动态加载数据,因此您必须使用selenium。检查下面的代码。注意,您必须安装selenium和
chromedrive
(解压文件并复制到python文件夹)
import time
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
url = "https://www.sofascore.com/pt/futebol/2018-09-18"
options = Options()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
driver = webdriver.Chrome(chrome_options=options)
driver.get(url)
time.sleep(3)
page = driver.page_source
driver.quit()
soup = BeautifulSoup(page, 'html.parser')
container = soup.find_all('div', attrs={
'class':'js-event-list-tournament-events'})
print(container)
或者您可以使用他们的json api。
import requests
url = 'https://www.sofascore.com/football//2018-09-18/json'
r = requests.get(url)
print(r.json())
发布于 2021-02-28 15:26:31
我也有同样的问题,下面的代码对我有效。必须安装Chromedriver!
import time
from bs4 import BeautifulSoup
from selenium import webdriver
chromedriver_path= "/Users/.../chromedriver"
driver = webdriver.Chrome(chromedriver_path)
url = "https://yourURL.com"
driver.get(url)
time.sleep(3) #if you want to wait 3 seconds for the page to load
page_source = driver.page_source
soup = bs4.BeautifulSoup(page_source, 'lxml')
这个
您可以像往常一样使用。
https://stackoverflow.com/questions/52687372
复制