Python爬虫代码优化:提高运行速度并修正错误

本文将分析一段Python爬虫代码中存在的错误,并提供详细的优化建议,最终给出优化后的代码示例。

原始代码:

import re
import requests
from bs4 import BeautifulSoup
from concurrent.futures import ThreadPoolExecutor

url = 'https://www.example.com/'
response = requests.get(url)
html = response.text

soup = BeautifulSoup(html, 'html.parser')

keywords = ['{typePython', '{type', '爬虫']
pattern = '|'.join(keywords)
regex = re.compile(pattern)

for keyword in keywords:
    result = regex.search(html)
    if result:
        print(result.group())

links = soup.find_all('a')
with ThreadPoolExecutor(max_workers=10) as pool:
    results = pool.map(requests.get, (link.get('href') for link in links if link.get('href').startswith('http')))
    for result in results:
        sub_html = result.text
        sub_result = regex.search(sub_html)
        if sub_result:
            print(sub_result.group())

代码错误:

  1. 在循环中使用 regex.search(html),每次循环都要重新搜索整个页面,浪费时间,应该在循环外先进行一次搜索,然后在循环中使用结果;
  2. 在使用线程池时,应该使用 result.text 而不是 result.content.decode(),因为后者会把内容以字节形式返回,需要手动解码,而前者会自动解码并返回字符串。

代码优化:

  1. 在使用线程池时,应该使用 Session 对象来管理请求,并且设置超时时间,以避免程序长时间等待;
  2. 可以使用正则表达式的 findall() 方法来一次性搜索整个页面,而不是每次循环都搜索一次,这样可以提高效率。

优化后的代码:

import re
import requests
from bs4 import BeautifulSoup
from concurrent.futures import ThreadPoolExecutor

url = 'https://www.example.com/'
session = requests.Session()
session.headers.update({'User-Agent': 'Mozilla/5.0'})
response = session.get(url, timeout=10)
html = response.text

soup = BeautifulSoup(html, 'html.parser')

keywords = ['{typePython', '{type', '爬虫']
pattern = '|'.join(keywords)
regex = re.compile(pattern)
result = regex.search(html)
if result:
    print(result.group())

links = soup.find_all('a')
with ThreadPoolExecutor(max_workers=10) as pool:
    responses = pool.map(session.get, (link.get('href') for link in links if link.get('href').startswith('http')), timeout=10)
    for response in responses:
        sub_html = response.text
        sub_results = regex.findall(sub_html)
        for sub_result in sub_results:
            print(sub_result)

通过以上优化,代码的运行速度将得到显著提升,并能够有效避免一些潜在的错误。

Python爬虫代码优化:提高运行速度并修正错误

原文地址: https://www.cveoy.top/t/topic/loLG 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录