【问题标题】:Finding location of class for BeautifulSoup查找 BeautifulSoup 的课程位置
【发布时间】:2019-02-23 17:50:59
【问题描述】:

我正在努力使用 BeautifulSoup。我想在[Transfermarkt][1]右侧的表格中抓取比赛的链接

到目前为止我是如何找到它的:

div1 = soup.find('div', {'class': 'large-4 columns'})
div2 = div1.find('div', {'class': 'box'})
table = div2.find('table')
table_body = table.find('tbody')
contest = table_body.find_all('a')

问题是这不够具体。我有时会发现双重值,这完全破坏了我的结构......

有没有更好的方法来定位这个确切的位置?

我需要的位置:“td”“class=no-border-links”中的“a”“title”

【问题讨论】:

    标签: html python-3.x web-scraping beautifulsoup


    【解决方案1】:

    在这种情况下最好使用select

    for title in soup.select('.large-4.columns td.no-border-links > a'):
        if title.text:
            print(title.text)
    

    输出将是

    Weltmeisterschaft 2014
    UEFA Champions League
    1.Bundesliga
    1.Bundesliga
    1.Bundesliga
    1.Bundesliga
    FC Bayern München
    1.Bundesliga
    UEFA Champions League
    1.Bundesliga
    1.Bundesliga
    1.Bundesliga
    Deutschland
    Deutschland
    Weltmeisterschaft 2018
    Weltmeisterschaft 2014
    Weltmeisterschaft 2010
    Europameisterschaft 2016
    Europameisterschaft 2012
    Weltmeisterschaft 2014
    U21-Europameisterschaft 2009
    UEFA Champions League
    1.Bundesliga
    Weltmeisterschaft 2010
    Deutschland
    UEFA Champions League
    UEFA Champions League
    UEFA Champions League
    UEFA Champions League
    UEFA Champions League
    UEFA Champions League
    UEFA Champions League
    UEFA Champions League
    UEFA Champions League
    UEFA Super Cup
    FC Bayern München
    FC Bayern München
    FC Bayern München
    Deutschland
    FIFA Klub-WM
    DFB-Pokal
    DFB-Pokal
    DFB-Pokal
    DFB-Pokal
    DFL-Supercup
    DFL-Supercup
    DFL-Supercup
    DFB-SuperCup
    DFB-Pokal
    U21-Europameisterschaft 2009
    

    【讨论】:

    • 谢谢,但有一些缺失值。 “德国”不见了,“拜仁慕尼黑”也不见了……它们不是头衔。 Deutschland 只在'no-border-links' 类下的href 中,而Bayern 在一个类中......我该如何实现这些例外?
    • @viktor 更新了我的答案。它失去了秩序,但它给了你想要的东西。
    • 嗯,还是不行。现在我得到了大约 70 个值......甚至沙尔克现在也在列表中,它只在页面左侧的桌子上......
    • 嗯。再次更新。我想我这次做对了。
    【解决方案2】:

    尝试以下操作以获得所需的内容:

    import re
    import requests
    from bs4 import BeautifulSoup
    
    URL = "https://www.transfermarkt.de/jumplist/erfolge/spieler/17259"
    
    res = requests.get(URL,headers={"User-Agent":"Mozilla/5.0"})
    soup = BeautifulSoup(res.text,"lxml")
    for items in soup.select(".table-header:contains('Alle Titel') + table tr"):
        if not items.find("a",string=re.compile("\w")):continue
        item = items.find("a",string=re.compile("\w")).text
        print(item)
    

    要获取链接,请尝试以下操作:

    import re
    import requests
    from bs4 import BeautifulSoup
    from urllib.parse import urljoin
    
    URL = "https://www.transfermarkt.de/jumplist/erfolge/spieler/17259"
    
    res = requests.get(URL,headers={"User-Agent":"Mozilla/5.0"})
    soup = BeautifulSoup(res.text,"lxml")
    for items in soup.select(".table-header:contains('Alle Titel') + table tr"):
        if not items.find("a",string=re.compile("\w")):continue
        item = items.find("a",string=re.compile("\w")).text
        try:
            link = urljoin(URL,items.select_one("a[href^='/']").get("href"))
        except AttributeError: link = ""
        print(item,link)
    

    【讨论】:

    • 谢谢,这行得通,但我还需要链接。总而言之,按结构化顺序...我已经在顶部更新了我的代码...有些玩家在这里喜欢这个:transfermarkt.de/jumplist/erfolge/spieler/143891完全破坏我的结构...
    • 查看更新。如果您需要更多帮助@viktor,请确保创建另一个帖子。
    【解决方案3】:

    尝试使用汤库中的select 函数,您可以在其中使用CSS selectors

    在你的情况下,你可以使用类似的东西-

    a_tags = soup.select("td[class='no-border-links'] > a")
    

    现在您可以使用text 属性对其进行迭代以获取标题。

    【讨论】:

    • 感谢您的回复!不幸的是,这不起作用..我已经有了标题。我需要相应俱乐部或比赛的链接...
    猜你喜欢
    • 2017-01-20
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-14
    • 2022-10-02
    • 2012-01-23
    • 2019-10-19
    相关资源
    最近更新 更多