在Python中编写网络爬虫循环,通常使用`for`循环或`while`循环。以下是使用`for`循环和`while`循环的示例:
使用`for`循环
import requests
from bs4 import BeautifulSoup
循环遍历网页列表
urls = ['http://example.com/page1', 'http://example.com/page2', 'http://example.com/page3']
for url in urls:
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
提取网页中的信息
例如:提取所有链接
links = soup.find_all('a')
for link in links:
print(link.get('href'))
使用`while`循环
import requests
from bs4 import BeautifulSoup
初始化变量
url = 'http://example.com/page1'
i = 1
while i <= 3:
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
提取网页中的信息
例如:提取所有链接
links = soup.find_all('a')
for link in links:
print(link.get('href'))
更新URL
url = f'http://example.com/page{i}'
i += 1
注意事项
确保在循环中更新循环变量,以避免无限循环。
使用`break`语句可以提前退出循环。
使用`try-except`语句处理网络请求可能出现的异常。
遵循网站的`robots.txt`规则,尊重网站的爬取策略。
以上示例展示了如何使用`for`循环和`while`循环进行网络爬虫的开发。请根据实际需要调整代码,并确保遵循网站的爬取规则