利用python爬取全国水雨情信息

分析.png

分析

我们没有找到接口,所以打算利用selenium来爬取。

代码

import datetime
import pandas as pd
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options #建议使用谷歌浏览器
import time
chrome_options = Options()
chrome_options.add_argument('--headless')
driver = webdriver.Chrome()


# 存储中英文对应的变量的中文名
word_dict = {"poiBsnm": "流域",
             "poiAddv": "行政区",
             "rvnm": "河名", 
             "stnm": "站名",
             "tm": "时间", 
             "zl": "水位(米)",
             "ql": "流量(立方米/秒)",
             "wrz": "警戒水位(米)"}

# 空df接收结果
rain_total = pd.DataFrame([])


url = 'http://xxfb.mwr.cn/sq_dxsk.html'
driver.get(url)
time.sleep(5)
infos = driver.find_elements_by_xpath("/html/body//tbody[@id='DataContainer']/tr")

# pd.set_option('display.max_columns', None)#所有列
# pd.set_option('display.max_rows', None)#所有行


# 列表提取
for info in infos:
    poiBsnm = info.find_element_by_xpath("./td[1]").text
    poiAddv = info.find_element_by_xpath("./td[2]").text
    rvnm = info.find_element_by_xpath("./td[3]").text
    stnm = info.find_element_by_xpath("./td[4]").text
    tm = info.find_element_by_xpath("./td[5]").text
    zl = info.find_element_by_xpath("./td[6]").text
    ql = info.find_element_by_xpath("./td[7]").text
    wrz = info.find_element_by_xpath("./td[8]").text
    
# 组成pandas对象
    rain_data = [[poiBsnm,poiAddv,rvnm,stnm,tm,zl,ql,wrz]]  
    rain_df = pd.DataFrame(data=rain_data,columns=list(word_dict.values()))
    rain_total = pd.concat([rain_total,rain_df])
    print(rain_total)
# 关闭浏览器
driver.close()

# 保存数据
data_str = datetime.datetime.now().strftime('%Y_%m_%d')
rain_total.to_csv("%s_全国水雨情信息.csv" % (data_str),index=None, encoding="GB18030")

结果

结果.png

反思

时间爬取出现了一点问题,我也很不理解,其次,循环哪里应该可以简洁代码,写的不是很好,第三,没有形成模块化的代码。还有就是谢谢崔工的支持。

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
爬取全国雨情站点的经纬度,你可以使用Python中的第三方库(如requests、BeautifulSoup等)来进行网页爬取和数据提取。以下是一个简单的示例代码,演示如何爬取全国雨情网站的站点经纬度: ```python import requests from bs4 import BeautifulSoup def get_station_coordinates(): url = 'http://www.hydroinfo.gov.cn/waterreport/realtime/list.html' response = requests.get(url) soup = BeautifulSoup(response.text, 'html.parser') table = soup.find('table', class_='table_data') rows = table.find_all('tr') station_coordinates = {} for row in rows[1:]: cells = row.find_all('td') station_name = cells[1].text.strip() latitude = cells[3].text.strip() longitude = cells[4].text.strip() station_coordinates[station_name] = (latitude, longitude) return station_coordinates # 示例用法 coordinates = get_station_coordinates() for station, (latitude, longitude) in coordinates.items(): print(f"站点:{station},经度:{longitude},纬度:{latitude}") ``` 上述代码中,我们首先发送HTTP请求获取全国雨情网站的页面内容。然后使用BeautifulSoup库解析HTML内容,并通过CSS选择器找到对应的数据表格。接下来,我们遍历表格的每一行,提取站点名称、经度和纬度数据,并将其存储在一个字典中。 请注意,这只是一个示例,实际的网站结构可能有所不同。你需要根据目标网站的具体结构和HTML标签进行相应的调整。 另外,请务必遵守网站的使用规定和爬虫规范,避免对网站造成不必要的负荷和干扰。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值