Python 爬虫代码详解:使用正则表达式和线程池搜索关键词
导入必要的库
import re # 正则表达式库 import requests # 网络请求库 from bs4 import BeautifulSoup # 解析HTML的库 from concurrent.futures import ThreadPoolExecutor # 线程池库
定义要爬取的网址和会话
url = 'https://www.example.com/' session = requests.Session() session.headers.update({'User-Agent': 'Mozilla/5.0'})
发送网络请求并获取HTML文本
response = session.get(url, timeout=10) html = response.text
使用BeautifulSoup库解析HTML文本
soup = BeautifulSoup(html, 'html.parser')
定义要搜索的关键词并编译正则表达式
keywords = ['{typePython', '{type', '爬虫'] pattern = '|' .join(keywords) regex = re.compile(pattern)
在HTML文本中查找关键词并打印结果
result = regex.search(html) if result: print(result.group())
使用线程池同时请求页面并在子页面中查找关键词
links = soup.find_all('a') with ThreadPoolExecutor(max_workers=10) as pool: # 使用map方法发起多个网络请求 responses = pool.map(session.get, (link.get('href') for link in links if link.get('href').startswith('http')), timeout=10) # 遍历每个响应并解析HTML文本并查找关键词 for response in responses: sub_html = response.text sub_results = regex.findall(sub_html) for sub_result in sub_results: print(sub_result)
原文地址: https://www.cveoy.top/t/topic/loMq 著作权归作者所有。请勿转载和采集!