首页 > 解决方案 > 使用 python 抓取具有多个部分的页面

问题描述

我想从这个网站上获取完整的队友名单。我知道如何在第一页使用 beautifoulsoup 来做到这一点,但是结果在很多页面中都被破坏了。有没有办法刮掉所有的零件?

谢谢!

标签: pythonpython-3.xweb-scrapingbeautifulsoup

解决方案


https://www.transfermarkt.co.uk/yvon-mvogo/profil/spieler/147051

https://www.transfermarkt.co.uk/steve-von-bergen/profil/spieler/4793

https://www.transfermarkt.co.uk/scott-sutter/profil/spieler/34520

上面给出了一些玩家资料的链接。您可以在 BeautifulSoup 中打开页面并对其进行解析以获取其中的所有链接。在后面写一个正则表达式,只过滤出满足上述模式的链接,并编写另一个函数从个人资料页面中提取信息

soup = BeautifulSoup(html_page,'html.parser')
for a in soup.find_all('a', href=True):
    m = re.search('/[a-z\-]+/profil/spieler/[0-9]+', a['href'])
    if m:
        found = m.group(0)
        print(found)

输出

/michael-frey/profil/spieler/147043
/yvon-mvogo/profil/spieler/147051
/scott-sutter/profil/spieler/34520
/leonardo-bertone/profil/spieler/194975
/steve-von-bergen/profil/spieler /4793
/alain-nef/profil/spieler/4945
/raphael-nuzzolo/profil/spieler/32574
/marco-wolfli/profil/spieler/4860
/moreno-costanzo/profil/spieler/41207
/jan-lecjaks/profil/spieler /62854
/alain-rochat/profil/spieler/4843
/christoph-spycher/profil/spieler/2871
/gonzalo -zarate/profil/spieler/
52731 /christian-schneuwly/profil/spieler/52556
/yuya-kubo/profil/spieler /186260
/alexander-farnerud/profil/spieler/10255
/salim-khelifi/profil/spieler/147049
/alexander-gerndt/profil/spieler/45881
/adrian-winter/profil/spieler/59681
/victor-palsson/profil/spieler/97241
/milan-gajic/profil/spieler/46928
/dusan-veskovac/profil/spieler/28705
/marco-burki/profil/spieler/172192
/elsad-zverotic/profil/spieler/25542
/pa-modou/profil/spieler/66449
/yoric-ravet/profil/spieler/82461

您可以遍历所有链接并调用从配置文件页面中提取所需信息的函数。希望这可以帮助

使用此链接。我通过检查按钮得到它

https://www.transfermarkt.co.uk/michael-frey/gemeinsameSpiele/spieler/147043/ajax/yw2/page/1

您可以更改末尾的数字以获取每个页面


推荐阅读