【问题标题】:Web scraping using beautiful soup Python使用漂亮的汤 Python 进行网页抓取
【发布时间】:2022-01-08 08:38:58
【问题描述】:

我正在尝试从网站上抓取一些数据 - https://boardgamegeek.com/browse/boardgame/page/1

在获得游戏名称和分数后,我还想打开这些页面中的每一个,了解每场游戏需要多少玩家。但是,当我进入每个游戏时,URL 都有一个唯一的编号。 例如:当我点击第一个游戏——Gloomhaven 时,它会打开页面——https://boardgamegeek.com/boardgame/**174430**/gloomhaven(唯一编号以粗体标记)。

    random_no = r.randint(1000,300000)
    url2 = "https://boardgamegeek.com/boardgame/"+str(random_no)+"/"+name[0]    
    page2 = requests.get(url2)
    if page2.status_code==200:
        print("this is it!")
        break

所以我生成了一个随机数并将其插入 URL 并读取响应。但是,即使是错误的数字也能给出正确的响应,但不会打开正确的页面。

这个唯一编号是什么?我怎样才能得到这方面的信息?或者我可以使用其他方法来获取我需要的信息吗?

提前致谢。

【问题讨论】:

  • 不要把注意力集中在与你要的信息无关的数字上——只要获取url,获取数据即可。
  • @mama 嗨,当我尝试使用没有此号码的 URL 时,我无法打开 URL,因此无法提取任何信息。

标签: python url web-scraping beautifulsoup


【解决方案1】:

试试这个

import requests
import bs4

s = bs4.BeautifulSoup(requests.get(
    url = 'https://boardgamegeek.com/browse/boardgame/page/1',
).content, 'html.parser').find('table', {'id': 'collectionitems'})

urls = ['https://boardgamegeek.com'+x['href'] for x in s.find_all('a', {'class':'primary'})]

print(urls)

【讨论】:

    猜你喜欢
    • 2022-01-20
    • 1970-01-01
    • 2018-10-19
    • 1970-01-01
    • 2015-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多