for循环requests.get 多个网站
时间: 2023-10-01 08:11:29 浏览: 93
您可以使用Python中的for循环和requests库来请求多个网站。具体步骤如下:
1. 导入requests库
```python
import requests
```
2. 创建一个包含多个网站链接的列表
```python
websites = ['http://www.example.com', 'http://www.google.com', 'http://www.github.com']
```
3. 使用for循环对列表中的每个网站链接进行请求
```python
for website in websites:
response = requests.get(website)
print(response.text) # 打印网站的HTML文本
```
在上述代码中,我们首先使用for循环遍历websites列表中的每个网站链接。然后,我们使用requests库的get方法来请求每个网站,并将响应对象存储在变量response中。最后,我们打印每个网站的HTML文本,以便查看其内容。
请注意,为了避免请求过于频繁而被网站屏蔽,您可能需要在每个请求之间添加一些延迟时间。可以使用Python的time模块来实现这一点,例如:
```python
import time
for website in websites:
response = requests.get(website)
print(response.text)
time.sleep(1) # 延迟1秒钟
```
这样,每个请求之间都会有1秒钟的延迟,从而保证请求不会过于频繁。