Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.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 网页抓取(足球赔率)_Python_Web Scraping_Beautifulsoup - Fatal编程技术网

Python 网页抓取(足球赔率)

Python 网页抓取(足球赔率),python,web-scraping,beautifulsoup,Python,Web Scraping,Beautifulsoup,我对网络抓取还不熟悉,现在我试着去理解它,以便与朋友们就德国德甲进行一场赌博比赛。(我们使用的平台是kicktipp.de)。我已经设法登录到该网站,并用python发布足球比赛结果。不幸的是,到目前为止,这些只是泊松分布的随机数。为了改善这一点,我的想法是从下载赔率。更确切地说,我试图下载确切结果的概率。问题从这里开始。到目前为止,我无法提取那些带有BeautifulSoup的。使用GoogleChrome,我试图了解我需要的html代码的哪一部分。但由于某些原因,我找不到有Beautiful

我对网络抓取还不熟悉,现在我试着去理解它,以便与朋友们就德国德甲进行一场赌博比赛。(我们使用的平台是kicktipp.de)。我已经设法登录到该网站,并用python发布足球比赛结果。不幸的是,到目前为止,这些只是泊松分布的随机数。为了改善这一点,我的想法是从下载赔率。更确切地说,我试图下载确切结果的概率。问题从这里开始。到目前为止,我无法提取那些带有BeautifulSoup的。使用GoogleChrome,我试图了解我需要的html代码的哪一部分。但由于某些原因,我找不到有BeautifulSoup的那些部分。 目前我的代码确实如下所示:

from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup

my_url = "https://sports.bwin.com/de/sports/4/wetten/fußball#categoryIds=192&eventId=&leagueIds=43&marketGroupId=&page=0&sportId=4&templateIds=0.8649061927316986"

# opening up connection, grabbing the page
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()

# html parsing
page_soup = soup(page_html, "html.parser")
containers1 = page_soup.findAll("div", {"class": "marketboard-event-
group__item--sub-group"})
print(len(containers1))
containers2 = page_soup.findAll("table", {"class": "marketboard-event-with-
header__markets-list"})
print(len(containers2))
从容器的长度我已经可以看到,要么它们包含比我预期的更多的物品,要么它们因为未知的原因是空的。。。希望你能指引我。提前谢谢

您可以与一起使用来刮取生成JavaScript内容的页面,因为这里就是这种情况

from selenium import webdriver
from bs4 import BeautifulSoup

url = "https://sports.bwin.com/de/sports/4/wetten/fußball#categoryIds=192&eventId=&leagueIds=43&marketGroupId=&page=0&sportId=4&templateIds=0.8649061927316986"
driver = webdriver.Chrome()
driver.get(url)
soup = BeautifulSoup(driver.page_source, 'html.parser')

containers = soup.findAll("table", {"class": "marketboard-event-with-header__markets-list"})
现在,
containers
真的有了我们想要的,tables元素,检查更多,很容易看到我们想要的文本在交替的
标签中,所以我们可以使用
zip
iter
创建一个结果和赔率的元组列表,交替的
divs
列表元素:

resultAndOdds = []    
for container in containers:
    divs = container.findAll('div')
    texts = [div.text for div in divs]
    it = iter(texts)
    resultAndOdds.append(list(zip(it, it)))

演示:


根据您希望数据是什么样的,您还可以使用以下内容获取每个表的标题:

titlesElements = soup.findAll("div", {"class":"marketboard-event-with-header__market-name"})
titlesTexts = [title.text for title in titlesElements]

它是否像您打印出
page\u soup.prettify()
时所期望的那样显示所有表格?另外,您是否考虑过使用请求而不是urllib.request?这无疑是很少出现的更好的想法之一。顺便说一句,driver.quit命令应该放在哪里?谢谢。您可以使用
driver.quit()
或。那将是在
soup
被创建之后。@Shahin我一意识到就编辑了,很抱歉!=)@HighwayJohn我很乐意帮忙!
titlesElements = soup.findAll("div", {"class":"marketboard-event-with-header__market-name"})
titlesTexts = [title.text for title in titlesElements]