【发布时间】:2021-12-05 10:19:11
【问题描述】:
我想在按下按钮“更多”[+] 次以加载所有游戏后获取此站点https://www.forebet.com/en/football-predictions 的 HTML。每次页面底部的更多[+] 按钮时,HTML 都会更改并显示更多足球比赛。如何获取加载所有足球比赛的页面的请求?
from bs4 import BeautifulSoup
import requests
leagues = {"EPL","UCL","Es1","De1","Fr1","Pt1","It1","UEL"}
class ForeBet:
#gets all games from the leagues on leagues returning the games on a string list
#game format is League|Date|Hour|Home Team|Away Team|Prob Home|Prob Tie| Prob Away
def get_games_and_probs(self):
response=requests.get('https://www.forebet.com/en/football-prediction')
soup = BeautifulSoup(response.text, 'html.parser')
results=list()
games = soup.findAll(class_='rcnt tr_0')+soup.findAll(class_='rcnt tr_1')
for game in games:
if(leagues.__contains__(game.find(class_='shortTag').text.strip())):
game=game.find(class_='shortTag').text+"|"+\
game.find(class_='date_bah').text.split(" ")[0]+"|"+ \
game.find(class_='date_bah').text.split(" ")[1]+"|"+ \
game.find(class_='homeTeam').text+"|"+\
game.find(class_='awayTeam').text+"|"+\
game.find(class_='fprc').findNext().text+"|"+\
game.find(class_='fprc').findNext().findNext().text+"|"+\
game.find(class_='fprc').findNext().findNext().findNext().text
print(game)
results.append(game)
return results
【问题讨论】:
-
问题到底是什么?
-
我想在按下按钮 x 次后获取 HTML
-
如果您查看源代码,您会看到
More [+]按钮调用了一个JavaScript 函数。你不能用 bs4 重新创建它——你需要一个(无头)浏览器,比如 Selenium 或 PhantomJS
标签: python web-scraping beautifulsoup request