这个问题紧随先前的问题。我想使用Python从投注网站中删除数据。我首先尝试遵循本教程,但问题是该网站tipico无法从瑞士获得。因此,我选择了另一个博彩网站:Winamax。在本教程中,首先检查网页tipico,以找到html文件中的下注率。在tipico网页中,它们存储在“ c_but_base c_but”类的按钮中,因此,通过编写以下几行,可以使用“美汤”模块保存和打印费率:
from bs4 import BeautifulSoup
import urllib.request
import re
url = "https://www.tipico.de/de/live-wetten/"
try:
page = urllib.request.urlopen(url)
except:
print(“An error occured.”)
soup = BeautifulSoup(page, ‘html.parser’)
regex = re.compile(‘c_but_base c_but’)
content_lis = soup.find_all(‘button’, attrs={‘class’: regex})
print(content_lis)
因此,我试图对网页Winamax进行同样的处理。我检查了页面,发现下注率存储在“ ui-touchlink-needsclick price单价”类的按钮中。请参见下面的代码:
from bs4 import BeautifulSoup
import urllib.request
import re
url = "https://www.winamax.fr/paris-sportifs/sports/1/7/4"
try:
page = urllib.request.urlopen(url)
except Exception as e:
print(f"An error occurred: {e}")
soup = BeautifulSoup(page, 'html.parser')
regex = re.compile('ui-touchlink-needsclick price odd-price')
content_lis = soup.find_all('button', attrs={'class': regex})
print(content_lis)
问题是它什么都不打印:Python找不到此类的元素(对吗?)。因此,我尝试打印汤对象,以查看BeautifulSoup函数的功能。我加了这条线
print(soup)
当打印出来(我没有表现出来的汤打印,因为实在是太长了),我注意到,这是不一样的文字,当我做了右键点击“检查”的Winamax网页中显示的内容。那么BeautifulSoup函数到底在做什么?如何使用BeautifulSoup从Winamax网站存储投注率?
编辑:我从来没有用html编码,我是Python的初学者,所以某些术语可能是错误的,这就是为什么某些部分用斜体表示的原因。
这是因为该网站JavaScript
用于显示这些详细信息,而BeautifulSoup本身并不与之交互JS
。
首先尝试找出您要抓取的元素是否存在于页面源中,如果可以的话,您可以抓取几乎所有内容!在您的情况下,按钮/跨度标签不在页面源中(意味着已隐藏或已通过脚本提取)
因此,我建议使用Selenium作为解决方案,并且尝试了该网站的基本知识。
这是我使用的代码:
from selenium import webdriver
option = webdriver.ChromeOptions()
option.add_argument('--headless')
option.binary_location = r'Your chrome.exe file path'
browser = webdriver.Chrome(executable_path=r'Your chromedriver.exe file path', options=option)
browser.get(r"https://www.winamax.fr/paris-sportifs/sports/1/7/4")
span_tags = browser.find_elements_by_tag_name('span')
for span_tag in span_tags:
print(span_tag.text)
browser.quit()
这是输出:
此输出中存在一些垃圾数据,但这是供您确定所需的内容和不需要的内容!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句