Python 爬虫实战:从百度百科提取词条信息
以下是一个简单的爬虫实例,可以爬取百度百科中'Python'词条的基本信息:
- 导入必要的库
import requests
from bs4 import BeautifulSoup
- 发送请求并获取页面内容
url = 'https://baike.baidu.com/item/Python/407313?fr=aladdin'
response = requests.get(url)
html = response.content.decode('utf-8')
- 解析页面内容并提取信息
soup = BeautifulSoup(html, 'html.parser')
basic_info = soup.select('.basicInfo-item')
for item in basic_info:
key = item.select('.name')[0].get_text().strip()
value = item.select('.value')[0].get_text().strip()
print(key + ': ' + value)
- 输出结果
中文名: Python
外文名: Python
开发者: 吉多·范罗苏姆
开发时间: 1989年
最新版本: 3.9.2
以上代码只是一个简单的示例,实际爬虫需要更加复杂的处理逻辑和数据清洗,同时还需要注意反爬虫机制。
AI 优化版
原文地址:https://www.cveoy.top/t/topic/m18q 著作权归作者所有。请勿转载和采集!