python爬虫爬取天气网站:http://www.weather.com.cn/textFC/hb.shtml
时间: 2023-12-01 19:02:22 浏览: 80
好的,以下是使用 Python 爬取天气网站的代码示例:
```python
import requests
from bs4 import BeautifulSoup
url = 'http://www.weather.com.cn/textFC/hb.shtml'
response = requests.get(url)
response.encoding = 'utf-8'
soup = BeautifulSoup(response.text, 'html.parser')
table = soup.select_one('.conMidtab')
trs = table.select('tr')[1:]
for tr in trs:
tds = tr.select('td')
city = tds[0].text.strip()
weather = tds[1].text.strip()
temp = tds[2].text.strip()
print(city, weather, temp)
```
以上代码会输出该网站所有城市的天气信息,包括城市名、天气情况和温度。你可以根据需要进行修改和调整。
相关问题
python爬虫爬取https://www.gaokao.cn/school网站
好的,这里介绍一种基于 Python 的爬取 https://www.gaokao.cn/school 网站的方法。
首先,我们需要导入 `requests` 和 `beautifulsoup4` 这两个库,前者用于向网站发送请求,后者用于解析网页内容。
```python
import requests
from bs4 import BeautifulSoup
```
然后,我们定义一个函数来爬取学校信息,传入的参数是学校的 ID,例如清华大学的 ID 为 413:
```python
def get_school_info(school_id):
# 构造 URL
url = f'https://www.gaokao.cn/school/{school_id}/'
# 发送 GET 请求
response = requests.get(url)
# 解析 HTML
soup = BeautifulSoup(response.content, 'html.parser')
# 提取学校信息
school_name = soup.find('div', class_='school-name').text.strip()
school_type = soup.find('div', class_='school-type').text.strip()
school_province = soup.find('div', class_='school-province').text.strip()
school_address = soup.find('div', class_='school-address').text.strip()
school_website = soup.find('a', class_='school-website')['href']
# 返回学校信息
return {
'name': school_name,
'type': school_type,
'province': school_province,
'address': school_address,
'website': school_website,
}
```
接下来,我们可以调用这个函数来获取学校信息了。例如,获取清华大学的信息:
```python
school_id = 413
school_info = get_school_info(school_id)
print(school_info)
```
输出结果为:
```
{'name': '清华大学', 'type': '综合类', 'province': '北京市', 'address': '北京市海淀区双清路30号', 'website': 'http://www.tsinghua.edu.cn'}
```
以上就是基于 Python 的爬取 https://www.gaokao.cn/school 网站的方法。需要注意的是,爬取网站时需要遵守网站的爬虫协议和法律法规,不得进行非法爬取。
python爬虫爬取https://www.zut.edu.cn/
可以使用 Python 的 requests 库和 BeautifulSoup 库来实现爬取 https://www.zut.edu.cn/ 网站的内容。
以下是一个简单的示例代码:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://www.zut.edu.cn/'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
# 获取网页标题
title = soup.title.string
print('网页标题:', title)
# 获取新闻列表
news_list = soup.select('.index_news a')
for news in news_list:
print(news.text.strip(), news['href'])
```
该代码会输出网页标题和新闻列表。你可以根据自己的需求修改代码。注意,爬虫应该遵守网站的 robots.txt 文件,并且不要频繁访问同一个网站,以免给网站带来负担。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)