python爬取中药数据
时间: 2023-07-30 13:12:27 浏览: 70
你可以使用 Python 中的 requests 和 BeautifulSoup 库来爬取中药数据。以下是一个简单的示例:
```python
import requests
from bs4 import BeautifulSoup
url = 'http://www.tcmmzxy.com/'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
herbs = soup.find_all('div', {'class': 'herb'})
for herb in herbs:
name = herb.find('div', {'class': 'name'}).text.strip()
description = herb.find('div', {'class': 'description'}).text.strip()
print(name + ': ' + description)
```
这个示例爬取了 http://www.tcmmzxy.com/ 网站上的中药数据,并打印每种中药的名称和简介。你可以根据需要修改代码来适应其他网站或数据来源。请注意,使用爬虫时请注意网站的爬取规则和法律法规,避免违法行为。
相关问题
python爬取天气数据_Python爬取历史天气数据
好的,您想了解如何用 Python 爬取历史天气数据,是吗?
首先,需要确定您想要爬取哪个地区的历史天气数据。一般来说,可以通过查询该地区的气象局或气象网站获取历史天气数据。
接着,可以使用 Python 中的 requests 库来发送 HTTP 请求,获取网页源代码。然后,可以使用 BeautifulSoup 库来解析网页源代码,提取出所需的天气数据。
以下是一个简单的示例代码,用于爬取中国天气网上北京市 2019 年 1 月份的历史天气数据:
``` python
import requests
from bs4 import BeautifulSoup
url = 'http://www.weather.com.cn/weather/101010100.shtml#dt'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
r = requests.get(url, headers=headers)
r.encoding = 'utf-8'
soup = BeautifulSoup(r.text, 'html.parser')
weather_list = soup.select('.t li')
for weather in weather_list:
date = weather.select('.time')[0].text
weather_detail = weather.select('.temp')[0].text.strip()
print(date, weather_detail)
```
以上代码中,我们首先通过 requests 库发送了一个 GET 请求,获取了北京市天气网的网页源代码。然后,使用 BeautifulSoup 库解析网页源代码,提取出了 2019 年 1 月份的天气数据,并打印输出了日期和天气详情。
需要注意的是,不同的网站网页结构不同,需要根据具体情况修改代码。同时,需要注意网站的 robots.txt 文件,不要过度访问网站,以免被封 IP 或其他限制。
python爬取高考数据
爬取高考数据是一个常见的数据获取任务,Python提供了许多库和工具来实现这个目标。以下是一个使用Python爬取高考数据的示例:
```python
import requests
from bs4 import BeautifulSoup
# 发送HTTP请求获取网页内容
url = "http://example.com" # 替换为你要爬取的网页URL
response = requests.get(url)
html_content = response.text
# 使用BeautifulSoup解析网页内容
soup = BeautifulSoup(html_content, "html.parser")
# 根据网页结构和标签选择器提取所需数据
data = soup.find("div", class_="exam-data").text # 替换为你要提取的数据所在的标签和类名
# 打印提取的数据
print(data)
```
请注意,上述代码仅为示例,实际爬取高考数据需要根据具体网页结构和数据位置进行相应的修改。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)