且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用beautifulsoup解析网站

更新时间:2023-11-26 23:14:46

显然,该页面仅在浏览器中调用后才加载赔率"部分.所以你可以使用 SeleniumChrome 驱动程序.

Apparently, the page only loades the "odds" parts once it is called in a browser. So you could use Selenium and Chrome driver.

请注意,您需要下载 Chrome 驱动程序并将驱动程序放在您的 .../python/ 目录中.确保选择匹配的驱动程序版本,即与您安装的 Chrome 浏览器版本匹配的 Chrome 驱动程序版本.

Note that you need to download the Chrome driver and place the driver in your .../python/ directory. Make sure you choose a matching driver version, meaning a version of Chrome driver that matches the version of the Chrome browser you have installed.

from bs4 import BeautifulSoup 
from urllib.request import urlopen 
import requests, time, traceback, random, csv, codecs, re, os

# Webdriver
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By

options = webdriver.ChromeOptions()
options.add_argument('log-level=3')
browser = webdriver.Chrome(chrome_options=options)

url = 'https://www.oddsportal.com/matches/tennis/20191114/'
browser.get(url)
soup = BeautifulSoup(browser.page_source, "html.parser")
info = soup.findAll('tr', {'class':'odd deactivate'})
print(info)