你能写一个爬取中国各省份大学数量的爬虫吗?
时间: 2023-12-10 12:37:07 浏览: 40
可以的,以下是一个简单的爬虫示例,使用 Python 语言和 BeautifulSoup 库:
```python
import requests
from bs4 import BeautifulSoup
# 请求URL
url = 'https://baike.baidu.com/item/%E4%B8%AD%E5%9B%BD%E5%A4%A7%E5%AD%A6%E5%88%97%E8%A1%A8'
html = requests.get(url).text
soup = BeautifulSoup(html, 'html.parser')
# 获取所有表格
tables = soup.find_all('table')
# 遍历表格
for table in tables:
# 获取表格标题
caption = table.find('caption')
if caption and '各省市高校数量' in caption.text:
# 获取表格行
rows = table.find_all('tr')
for row in rows:
# 获取表格列
cols = row.find_all('td')
if cols:
province = cols[0].text.strip()
count = cols[1].text.strip()
print(province, count)
```
这个爬虫会爬取百度百科上的“中国大学列表”页面,提取“各省市高校数量”表格中的数据,并输出每个省份的高校数量。请注意,这个爬虫只是一个示例,如果您需要爬取其他网站的数据,需要根据具体情况进行修改。