首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Web抓取循环python问题

Web抓取循环python问题
EN

Stack Overflow用户
提问于 2018-09-16 23:52:49
回答 1查看 79关注 0票数 2

我是一名python新手,想知道是否有人能够用下面的webscraping脚本指出我哪里出了问题。

我试图递归地循环遍历匹配列表,为每个匹配提取一个累积值(度量)。

我的问题是,它每次都返回完全相同的值。

我已经尝试添加注释来解释我的每个观点,感谢任何帮助。

代码语言:javascript
复制
#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()           `
EN

回答 1

Stack Overflow用户

发布于 2018-09-17 00:00:54

我认为您需要更改print(cumul_match_shots)的缩进(并将其移动一点),就像在当前状态中一样-它将始终为您提供(print)上一个for循环的值。

我不确定您是否有正确的位置再次将值重置为0。目前看来,它将是所有比赛的积分值。

至于match -它应该是ok的,因为您不需要在for循环中修改它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52355845

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档