Python 循环url';与';对于';循环和字符串与整数问题

Python 循环url';与';对于';循环和字符串与整数问题,python,python-3.x,url,web-scraping,beautifulsoup,Python,Python 3.x,Url,Web Scraping,Beautifulsoup,尝试找出如何使用“for”循环(或任何其他循环)循环URL 我在Howlongtobeat.com上抓取数据url的结构如下: 如果只有“id=”结尾处的数字发生变化,如何使字符串结尾处的数字发生变化 page_number = range (38040, 38060) url = 'https://howlongtobeat.com/game.php?id={page_number}' 这不起作用,因为我没有添加到字符串 及 不起作用是因为我犯了这个错误 TypeError: must

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

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

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

page_number = range (38040, 38060)

url = 'https://howlongtobeat.com/game.php?id={page_number}'
这不起作用,因为我没有添加到字符串

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

 TypeError: must be str, not range
供参考使用beautifulsoup和csv writer废弃数据并将其写入csv

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

谢谢

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

井号的可能重复项是一个范围,而不是整数。你必须循环通过它
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