使用“for”循环和字符串与整数问题循环url

2024-09-28 05:16:27 发布

您现在位置:Python中文网/ 问答频道 /正文

尝试找出如何使用“for”循环(或任何其他循环)循环URL

我在Howlongtobeat.com上抓取数据url的结构如下:

https://howlongtobeat.com/game.php?id=38050

如果只有“id=”结尾处的数字发生变化,如何使字符串结尾处的数字发生变化

page_number = range (38040, 38060)

url = 'https://howlongtobeat.com/game.php?id={page_number}'

这不起作用,因为我没有添加到字符串

url = 'https://howlongtobeat.com/game.php?id=' + page_number 

不起作用是因为我犯了这个错误

 TypeError: must be str, not range

供参考使用beautifulsoup和csv writer废弃数据并将其写入csv

我是这方面的初学者,所以从顶部开始

谢谢


Tags: csv数据字符串httpscomidgameurl
1条回答
网友
1楼 · 发布于 2024-09-28 05:16:27
from bs4 import BeautifulSoup

url = 'https://howlongtobeat.com/game.php?id='

for page in range(38040, 38060):
    new_url = url + str(page)
    print(new_url)

输出:

C:\Users\siva\Desktop>python test.py
https://howlongtobeat.com/game.php?id=38040
https://howlongtobeat.com/game.php?id=38041
https://howlongtobeat.com/game.php?id=38042
https://howlongtobeat.com/game.php?id=38043
https://howlongtobeat.com/game.php?id=38044
https://howlongtobeat.com/game.php?id=38045
https://howlongtobeat.com/game.php?id=38046
https://howlongtobeat.com/game.php?id=38047
https://howlongtobeat.com/game.php?id=38048
https://howlongtobeat.com/game.php?id=38049
https://howlongtobeat.com/game.php?id=38050
https://howlongtobeat.com/game.php?id=38051
https://howlongtobeat.com/game.php?id=38052
https://howlongtobeat.com/game.php?id=38053
https://howlongtobeat.com/game.php?id=38054
https://howlongtobeat.com/game.php?id=38055
https://howlongtobeat.com/game.php?id=38056
https://howlongtobeat.com/game.php?id=38057
https://howlongtobeat.com/game.php?id=38058
https://howlongtobeat.com/game.php?id=38059

相关问题 更多 >

    热门问题