在Python爬虫开发中,优化代码可以从多个方面进行。以下是一些常见的优化策略:
threading
或multiprocessing
库来并行处理请求,提高爬虫的抓取速度。asyncio
库进行异步IO操作,减少等待时间。requests
库的Session
对象)来复用连接,减少建立和关闭连接的开销。try-except
块捕获和处理异常,确保爬虫的稳定性。以下是一个简单的爬虫示例,展示了上述优化策略的应用:
import requests
from bs4 import BeautifulSoup
import asyncio
import aiohttp
import time
class WebScraper:
def __init__(self, proxies=None):
self.session = requests.Session()
if proxies:
self.session.proxies = proxies
self.headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
async def fetch(self, url):
async with aiohttp.ClientSession() as session:
async with session.get(url, headers=self.headers) as response:
return await response.text()
def parse(self, html):
soup = BeautifulSoup(html, 'html.parser')
# 解析逻辑
return parsed_data
async def run(self, urls):
tasks = [self.fetch(url) for url in urls]
htmls = await asyncio.gather(*tasks)
for html in htmls:
data = self.parse(html)
# 存储数据
self.save_data(data)
time.sleep(1) # 设置请求间隔
def save_data(self, data):
# 存储数据到数据库或文件
pass
if __name__ == "__main__":
proxies = {
'http': 'http://proxy.example.com:8080',
'https': 'http://proxy.example.com:8080'
}
scraper = WebScraper(proxies=proxies)
urls = [
'http://example.com/page1',
'http://example.com/page2'
]
asyncio.run(scraper.run(urls))
通过模块化设计、多线程/多进程、异步IO、连接池、代码简洁性优化、反爬虫策略优化、数据存储优化以及错误处理和日志记录等手段,可以显著提高Python爬虫的性能和稳定性。