Py学习  »  Python

Web刮削循环python问题

smk90 • 4 年前 • 519 次点击  

我是一个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()           `
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/38092
 
519 次点击  
文章 [ 1 ]  |  最新文章 4 年前
opalczynski
Reply   •   1 楼
opalczynski    5 年前

我认为您需要更改打印(累积匹配放炮)的缩进(并将其稍高一点),就像在当前状态下一样-它将始终为您提供(打印)上一个for循环的值。

我不确定你是否有一个正确的地方,重新设置值为0。目前看来,它将是所有比赛中得分的累积值。

至于match,它应该是OK,因为您不会在for循环中修改它。