问题描述
我是python新手,想知道是否有人可以通过以下网络抓取脚本来突出说明我在哪里出错了。
我试图递归地遍历匹配项列表,以获取每个匹配项的累积值(度量)。
我的问题是,它每次都返回完全相同的值。
我尝试添加注释以解释我的每一个观点,对任何帮助表示赞赏。
#use Selenium & Beautiful Soup
from selenium import webdriver
import time
from bs4 import BeautifulSoup
#define URL/driver
my_url = "https://www.bet365.com/#/IP/"
driver = webdriver.Edge()
driver.get(my_url)
#allow a sleep of 10 seconds
time.sleep(10)
#parse the page
pSource= driver.page_source
soup = BeautifulSoup(pSource, "html.parser")
#containers tag - per match
containers = soup.findAll("div", {"class": "ipn-TeamStack "})
for container in containers:
#Total Match Shots
cumul_match_shots = 0
match = container.find_all('div')
for data in soup.findAll('div',{'class':'ml1-SoccerStatsBar '}):
for result in data.find_all('span'):
a = result.text
if len(a) > 0:
cumul_match_shots += int(a)
#print out values
print(match)
print(cumul_match_shots)
#close the webpage
driver.close() `
1楼
我认为您需要更改print(cumul_match_shots)的缩进(并将其缩进一点),就像在当前状态下一样-它将始终为(打印)最后一个for循环中的值。
而且我不确定您是否有合适的位置再次将该值重置为0。 目前看来,这将是所有比赛中总得分的累积值。
至于匹配-应该没问题,因为您无需在for循环中对其进行修改。