我是一名python新手,想知道是否有人能够用下面的webscraping脚本指出我哪里出了问题。
我试图递归地循环遍历匹配列表,为每个匹配提取一个累积值(度量)。
我的问题是,它每次都返回完全相同的值。
我已经尝试添加注释来解释我的每个观点,感谢任何帮助。
#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() `
发布于 2018-09-17 00:00:54
我认为您需要更改print(cumul_match_shots)的缩进(并将其移动一点),就像在当前状态中一样-它将始终为您提供(print)上一个for循环的值。
我不确定您是否有正确的位置再次将值重置为0。目前看来,它将是所有比赛的积分值。
至于match -它应该是ok的,因为您不需要在for循环中修改它。
https://stackoverflow.com/questions/52355845
复制相似问题