用python 在http://sd.shaniaoyun.com/weihai/project_list.html网站上获取工程名称 并列表
时间: 2023-12-18 21:05:36 浏览: 35
好的,我明白了。您可以使用 Python 的 requests 库和 BeautifulSoup 库来获取网页内容,并解析 HTML 标签来获取工程名称列表。以下是示例代码:
```python
import requests
from bs4 import BeautifulSoup
url = 'http://sd.shaniaoyun.com/weihai/project_list.html'
response = requests.get(url)
html = response.text
soup = BeautifulSoup(html, 'html.parser')
project_names = []
projects = soup.select('.project-name') # 选择 class 为 `project-name` 的标签
for project in projects:
name = project.get_text().strip()
project_names.append(name)
print(project_names)
```
这段代码会获取页面中所有 class 为 `project-name` 的标签,并将其文本内容添加到 `project_names` 列表中。最后打印这个列表来输出工程名称。注意,这个网站可能使用了反爬措施,需要添加一些额外的处理来避免被禁止访问。
相关问题
爬取山东省各个县级市的2021年3月历史天气数据python
你可以使用 Python 中的 requests 和 BeautifulSoup 库爬取历史天气数据。具体步骤如下:
1. 打开网站 http://lishi.tianqi.com/shandong/index.html
2. 找到需要爬取的县级市的历史天气页面,比如济南市的历史天气页面为 http://lishi.tianqi.com/jinan/202103.html
3. 使用 requests 库发送 GET 请求获取页面的 HTML 内容。
4. 使用 BeautifulSoup 库解析 HTML 内容,并提取出需要的数据。
5. 将数据保存到本地文件中,可以使用 csv 或者 Excel 格式保存。
下面是一份示例代码,你可以根据需要进行修改和完善:
```python
import requests
from bs4 import BeautifulSoup
import csv
# 历史天气数据页面的 URL
url_template = 'http://lishi.tianqi.com/{}/202103.html'
# 需要爬取的县级市列表
cities = ['jinan', 'qingdao', 'zibo', 'zaozhuang', 'dongying', 'yantai', 'weifang', 'jining', 'taian', 'weihai', 'rizhao', 'laiwu', 'linyi', 'dezhou', 'liaocheng', 'heze', 'binzhou']
# 请求头部信息
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36'
}
# 遍历每个县级市,爬取历史天气数据
for city in cities:
# 构造实际的历史天气数据页面 URL
url = url_template.format(city)
# 发送 GET 请求,获取页面内容
response = requests.get(url, headers=headers)
# 使用 BeautifulSoup 解析页面内容
soup = BeautifulSoup(response.text, 'html.parser')
# 找到历史天气数据表格
table = soup.find('div', {'class': 'tqtongji2'})
# 找到表格中的所有行
rows = table.findAll('tr')
# 创建 CSV 文件,用于保存历史天气数据
with open(f'{city}.csv', 'w', newline='', encoding='utf-8') as csvfile:
writer = csv.writer(csvfile)
# 遍历表格中的每一行,提取出日期和天气数据
for row in rows[1:]:
columns = row.findAll('td')
date = columns[0].text.strip()
weather = columns[1].text.strip()
# 将日期和天气数据写入 CSV 文件中
writer.writerow([date, weather])
```
这个示例代码将爬取山东省各个县级市在 2021 年 3 月的历史天气数据,并将数据保存到 CSV 文件中。你可以根据需要进行修改和完善。
山东省县级市的拼音列表python
可以使用Python中的字典来存储山东省县级市及其对应的拼音,示例代码如下:
```python
shandong_cities = {
"济南市": "jinan",
"青岛市": "qingdao",
"淄博市": "zibo",
"枣庄市": "zaozhuang",
"东营市": "dongying",
"烟台市": "yantai",
"潍坊市": "weifang",
"济宁市": "jining",
"泰安市": "taian",
"威海市": "weihai",
"日照市": "rizhao",
"莱芜市": "laiwu",
"临沂市": "linyi",
"德州市": "dezhou",
"聊城市": "liaocheng",
"滨州市": "binzhou",
"菏泽市": "heze"
}
```
可以根据需要修改拼音的格式,比如全部大写或者首字母大写等。