【问题标题】:Looping through an element in BeautifulSoup, but outputting only childs of said element遍历 BeautifulSoup 中的元素,但仅输出所述元素的子元素
【发布时间】:2019-09-19 09:22:17
【问题描述】:

我目前的问题是确定如何使用 BeautifulSoup 和 Selenium 来抓取一个名为 Rocket League eSports 的电子竞技网站。

我能够找到数据并使用 Selenium,因为他们网页上使用了脚本。然后我使用 BeautifulSoup 来获取数据。从这里我可以导出所有团队名称,但是当我添加它时,我的列表中一直显示“无”。

from selenium import webdriver
from bs4 import BeautifulSoup
#import soupsieve
import time

#create a Google Chrome session
browser = 
webdriver.Chrome(executable_path='/home/jdr1018/chromedriver')

#maximizes Google Chrome window
browser.maximize_window()

#fetches the URL
browser.get('https://www.rocketleagueesports.com/stats/')

#pause to allow page to load
time.sleep(4)

#search the container and find all elements with h5 tag to print 
given elements
#container = browser.find_elements_by_tag_name('h5')

#hand over Selenium page source to Beautiful BeautifulSoup
soup_source = BeautifulSoup(browser.page_source, 'lxml')

namelist = [] #empty list for Team names

winpercentlist = [] #empty list for Win Percentage

rocketleaguedict = {} #empty dict for namelist + winpercentlist

#using XPath to find h5 element with class name and assinging it to 
teamnames
elements = browser.find_elements_by_xpath('//h5[@class="name"]/a')
teamnames = [element.text for element in elements]
#loop through team names to get each individual team name
for name in teamnames:
    #if statement to determine if name is already in the list
    if name in namelist:
        #append each team name through loop into empty list.
        pass
    else:
        namelist.append(name)
#return namelist to verify
return namelist
#for i in container:
   #print(i.get_attribute("innerHTML"))

#once program is done close Google Chrome
browser.close()}

我的输出类似于这样:

['CHIEFS ESPORTS CLUB']
['CHIEFS ESPORTS CLUB', 'NRG ESPORTS']
['CHIEFS ESPORTS CLUB', 'NRG ESPORTS', 'ICON ESPORTS']
['CHIEFS ESPORTS CLUB', 'NRG ESPORTS', 'ICON ESPORTS', 'RENAULT SPORT 
TEAM VITALITY']
['CHIEFS ESPORTS CLUB', 'NRG ESPORTS', 'ICON ESPORTS', 'RENAULT SPORT 
TEAM VITALITY', 'ERODIUM']
['CHIEFS ESPORTS CLUB', 'NRG ESPORTS', 'ICON ESPORTS', 'RENAULT SPORT 
TEAM VITALITY', 'ERODIUM', 'LOWKEY ESPORTS'] ...

这不完全是,但关键是它们是一堆“无”,我无法弄清楚为什么。

【问题讨论】:

    标签: python selenium selenium-webdriver web-scraping beautifulsoup


    【解决方案1】:

    您可以使用正则表达式和请求来获取团队名称。正则表达式可能会变得更加高效(我会很感激这方面的指导)

    import requests
    import re
    
    res = requests.get('https://www.rocketleagueesports.com/ajax/standings-script/?league=7-57d5ab4-qm0qcw&season=7-cab6afe099-06tjgk&region=0&stage=7-57d5ab4-g1dsq3')
    r = re.compile(r'name: "((?:(?!").)*)')
    teams = r.findall(res.text)
    

    输出示例:


    正则表达式:

    查看正则表达式和解释here

    它基本上以脚本标签中的字符串为目标,格式为name: "TeamName"。消极的前瞻是通过在队名之后的 " 处停止,而不是在最后一个队名之后的 " 处结束一场长比赛,以确保我将每个队名作为一个组。

    其他参考资料:

    1. https://www.regular-expressions.info/tutorial.html
    2. https://www.regular-expressions.info/lookaround.html

    【讨论】:

      【解决方案2】:

      使用这个:

      elements = browser.find_elements_by_xpath('//h5[@class="name"]/a')
      teamnames = [element.text for element in elements]
      

      解释为什么你的方法不起作用:

      您的解决方案有许多None,因为“G”、“G/GM”等列下的值也具有相同的 html 标记名和类。

      因此,teamnames 是一个包含数字的元素列表,其中没有 <a href>...</a> html 内容。当这样的元素不存在(link to BeautifulSoup documentation on find())时,调用方法name.find('a') 会返回None,因此您会得到一系列6 个None

      【讨论】:

      • 这实际上在我使用 xpath 时效果很好,但是在我的打印语句中,因为它通过循环重复每个“团队名称”,它一次添加一个。为了解决这个问题,我想做一个返回,所以它只在循环结束时显示完整列表。有没有办法让它返回工作,因为我现在得到的错误说语法返回在函数之外,但我看不出它是如何工作的。 - - 我更新了上面的代码以显示我现在拥有的内容。
      • 只需取消缩进print(namelist),它应该会在添加完所有团队名称后打印出来。
      猜你喜欢
      • 1970-01-01
      • 2016-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-23
      • 2012-12-03
      • 1970-01-01
      相关资源
      最近更新 更多