python爬虫爬取官方公开信息实例
时间: 2023-11-21 17:05:24 浏览: 284
好的,让我来回答你的问题。关于Python爬虫爬取官方公开信息,可以参考以下实例:
1. 爬取百度新闻的标题和链接信息:
```python
import requests
from bs4 import BeautifulSoup
url = "http://news.baidu.com/"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
for news in soup.select(".ulist.focuslistnews > li"):
title = news.h3.text
link = news.a["href"]
print(title, link)
```
2. 爬取天气信息:
```python
import requests
from bs4 import BeautifulSoup
url = "http://www.weather.com.cn/weather/101010100.shtml"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
weather = soup.select(".t > li")
for i in range(7):
date = weather[i].h1.text
detail = weather[i].p.text
print(date, detail)
```
希望对你有帮助!
阅读全文