【问题标题】:How to loop through urls in python with beautifulsoup in python如何在python中使用beautifulsoup在python中循环url
【发布时间】:2020-04-26 07:46:10
【问题描述】:

所以我有这段代码,可以从某个网站提取进球数。我想循环它,这样我就可以获得每个玩家的信息,例如:https://www.futbin.com/20/player/1,然后是https://www.futbin.com/20/player/2,然后是https://www.futbin.com/20/player/3,直到 5000。我该怎么做?这是我如何获取目标信息的代码。

url = 'https://www.futbin.com/20/player/143/cristiano-ronaldo'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
googclose = soup.find_all(class_='ps4-pgp-data')
hi=soup.find_all('div', attrs={'class':'ps4-pgp-data'})[4]

【问题讨论】:

  • 接受发送的评论(代码需要格式化以便更易读)
  • 所以如果我正确理解了您的问题,那么您可以创建一个玩家姓名或 ID 列表,例如 ['1','2','3'],使用变量 player_id 遍历列表并使用 @ 创建一个 url 987654328@ 和其余代码在循环中保持不变。

标签: python html url beautifulsoup


【解决方案1】:
all_goals=[] 

for i in range(1,51):
    url = 'https://www.futbin.com/20/player/{}'.format(i)
    response = requests.get(url) 
    soup = BeautifulSoup(response.text, 'html.parser')
    goal=soup.find_all('div', attrs={'class':'ps4-pgp-data'})[4].text.strip()
    all_goals.append(goal)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-27
    • 2019-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-01
    相关资源
    最近更新 更多