Python环境介绍
Python3.11,编译器Pycharm 2022.3
报错原因分析
对应Python3.11版本的Pandas无法兼容低版本的xls。
解决方案
- 以csv格式输出。
- 以xlsx格式输出。更改后可能会出现报错,没有安装openxls库,这时,只需安装该库即可正常运行。
更正后代码
# 水情日报 数据下载
# Water information data download
# Water information url
# http://61.163.88.227:8006/hwsq.aspx?sr=0nkRxv6s9CTRMlwRgmfFF6jTpJPtAv87
# Design by HanLin
# Parameters start =======
# Start date (include)开始日期
Start_date = '2003-05-20'
# End date (include)结束日期
End_date = '2003-06-24'
# Result save path保存路径
# (Under the Download_data folder)
Save_path = r'F:\water' #自己更改一下
# Parameters end =======
import warnings
warnings.filterwarnings('ignore')
import os
import sys
import numpy as np
import pandas as pd
import bs4
from bs4 import BeautifulSoup
import requests
import datetime
Water_info_url=r'http://61.163.88.227:8006/hwsq.aspx?sr=0nkRxv6s9CTRMlwRgmfFF6jTpJPtAv87'
# Initial login header
# In order to achieve cookie
header_first={
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,zh-TW;q=0.6',
'Cache-Control': 'max-age=0',
'Connection': 'keep-alive',
'Host': '61.163.88.227:8006',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.64',
}
# Secondary head
# In order to send a search request发送搜索请求
header_next={
'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,zh-TW;q=0.6',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Host': '61.163.88.227:8006',
'Origin': r'http://61.163.88.227:8006',
'Referer': 'http://61.163.88.227:8006/hwsq.aspx?sr=0nkRxv6s9CTRMlwRgmfFF6jTpJPtAv87',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.53 Safari/537.36 Edg/103.0.1264.37',
}
# Data to be transferred要传输的数据
# Use Form Data
postdata={
'ctl00$ScriptManager1': 'ctl00$ScriptManager1|ctl00$ContentLeft$Button1',
'__EVENTTARGET': '',
'__EVENTARGUMENT': '',
'ctl00$ContentLeft$menuDate1$TextBox11': '2022-06-13',
'__VIEWSTATE':'/wEPDwULLTEwMDI5NzA1NzkPZBYCZg9kFgICAw9kFgICBQ9kFgJmD2QWAgIBD2QWAgIBDxYCHglpbm5lcmh0bWwF60w8dGFibGUgd2lkdGg9Ijk4JSIgYm9yZGVyPSIwIiBjZWxscGFkZGluZz0iMCIgY2VsbHNwYWNpbmc9IjEiIGJnY29sb3I9IiNEMUREQUEiIGFsaWduPSJjZW50ZXIiPjx0cj48dGQgaGVpZ2h0PSI0MCIgYmFja2dyb3VuZD0ic2tpbi9pbWFnZXMvbmV3bGluZWJnMy5naWYiPjx0YWJsZSB3aWR0aD0iOTglIiBib3JkZXI9IjAiIGNlbGxzcGFjaW5nPSIwIiBjZWxscGFkZGluZz0iMCI+PHRyPjx0ZCBhbGlnbj0iY2VudGVyIj48ZGl2IGNsYXNzPSdmaXJzdFRpdGxlJz7msLTmg4Xml6XmiqU8L2Rpdj48ZGl2IGNsYXNzPSdzZWNUaXRsZSc+MjAyMi0wNi0yNjwvZGl2PjwvdGQ+PC90cj48L3RhYmxlPjwvdGQ+PC90cj48L3RhYmxlPjx0YWJsZSB3aWR0aD0iOTglIiBib3JkZXI9IjAiIGNlbGxwYWRkaW5nPSIyIiBjZWxsc3BhY2luZz0iMSIgYmdjb2xvcj0iI0QxRERBQSIgYWxpZ249ImNlbnRlciIgc3R5bGU9Im1hcmdpbi10b3A6OHB4IiBjbGFz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gICAgICAgICAgICAgICAgICAgICAgICA8L3RkPjx0ZD4yNi45NjwvdGQ+PHRkPjMwMjA8L3RkPjx0ZD4qNC4wMzwvdGQ+PC90cj48dHIgYWxpZ249ImNlbnRlciIgYmdjb2xvcj0iI0ZGRkZGRiI+PHRkPum7hOaysyAgICAgICAgICAgICAgICAgICAgICAgICAgPC90ZD48dGQ+5Yip5rSlICAgICAgICAgICAgICAgICAgICAgICAgICA8L3RkPjx0ZD4xMC40PC90ZD48dGQ+MjcxMDwvdGQ+PHRkPio2Ljc4PC90ZD48L3RyPjwvdGFibGU+PC90ZD48L3RyPjwvdGFibGU+ZGQX7a8AK6EFHourJJ2xgJ7zGGznTXSzg7yZvMnSVM0/Mw==',
'__VIEWSTATEGENERATOR': 'E4DC7756',
'__EVENTVALIDATION': '/wEdAAPgMurU7dGbCJhtK8P1Nstr9DkLBAR+UXBBGQ1m5cY+HY5Ggl8DGIT46Qo2GBY6Yh4fpd+LHAI2ihOULO1i+2DgH5H5WRTn9WanmVpiptZGnA==',
'__ASYNCPOST': 'true',
'ctl00$ContentLeft$Button1': '查询',
}
# Parameters that don't seem to be useful
param={
'sr': '0nkRxv6s9CTRMlwRgmfFF6jTpJPtAv87'
}
def Generate_date_list(start_date,end_date):
date_lists=[]
start_date=datetime.datetime.strptime(start_date,'%Y-%m-%d')
end_date=datetime.datetime.strptime(end_date,'%Y-%m-%d')
# Make sure the start time is less than the end time#确保开始时间小于结束时间
if start_date > end_date:
print('Start time is greater than end time !')
sys.exit(0)
else:
while(start_date <= end_date):
date_lists.append(datetime.datetime.strftime(start_date,'%Y-%m-%d'))
start_date=start_date+datetime.timedelta(days=1)
return date_lists
# Generate postdata corresponding to the date
def Generate_date_related_postdata(postdata,date):
postdata['ctl00$ContentLeft$menuDate1$TextBox11']=date
def Information_extraction(response):
response_lists=[]
# Parsing html
response = BeautifulSoup(response,'html.parser')
# Data cleaning
for single_response in response.find_all('tr'):
if len(single_response) == 5:
if isinstance(single_response.contents[0],bs4.element.Tag):
response_contents_lists=[]
for single_response_content in single_response.contents:
single_response_content_cache=single_response_content.contents
if len(single_response_content_cache)!=0:
response_contents_lists.append(single_response_content_cache[0])
response_lists.append(response_contents_lists)
return response_lists
def Generate_excel(information_lists):
column_header=information_lists[0]
Result_excel=pd.DataFrame(information_lists[1:],dtype=str,columns=column_header)
return Result_excel
def Result_save(save_path,result_excel,date):
dir_name = 'Download_data'
file_path = os.path.join(save_path,dir_name)
# Check if the folder is in
if not os.path.exists(file_path):
os.makedirs(file_path)
file_name = f'{date}.xlsx' ## "xls" was replaced by "xlsx".
file_path = os.path.join(file_path,file_name)
result_excel.to_excel(file_path,header=True,index=True)
if __name__=='__main__':
date_lists = Generate_date_list(Start_date,End_date)
session = requests.session()
First_response = session.post(Water_info_url,headers=header_first)
for date_list in date_lists:
Generate_date_related_postdata(postdata,date_list)
Next_response = session.post(Water_info_url, headers=header_next, data=postdata)
Next_response.raise_for_status()
Next_response.encoding = Next_response.apparent_encoding
# Get the returned infomation
Next_response = Next_response.text
Information_lists = Information_extraction(Next_response)
Result_excel = Generate_excel(Information_lists)
Result_save(Save_path,Result_excel,date_list)
print(f'Date : {date_list} ,data download is complete !')
本文转载自: https://blog.csdn.net/weixin_46629224/article/details/130140478
版权归原作者 冬_冬_ 所有, 如有侵权,请联系我们删除。
版权归原作者 冬_冬_ 所有, 如有侵权,请联系我们删除。