酷代码 AI
菜单
全部AI  /  热门  /  AI写代码神器
服务商

python 批量请求地址返回状态码

在Python中,你可以使用requests库来批量请求多个地址并获取其返回的状态码。以下是一个简单的示例: ```python import requests urls = [ 'http://example.com', 'http://example.org', 'http://example.net', # 更多的URL... ] for url in urls: response = requests.get(url) print(f'URL: {url}, 状态码: {response.status_code}') ``` 在这个例子中,我们首先导入了requests库,然后定义了一个包含多个URL的列表。接着,我们遍历这个列表,对每个URL发起GET请求,并打印出该URL和其对应的状态码。 注意,对于大量的请求,应确保遵守网站的robots.txt规则,避免对服务器造成过大压力,并考虑添加适当的延时以防止被封禁。例如: ```python import time import requests urls = [...] for url in urls: response = requests.get(url) print(f'URL: {url}, 状态码: {response.status_code}') time.sleep(1) # 每次请求后暂停1秒 ``` 如果需要处理更复杂的异步批量请求,可以使用如grequests、aiohttp等库进行异步IO操作,提高效率。 [2024-01-22 10:31:15 | AI写代码神器 | 271点数解答]

相关提问