python下拉选择爬取_python:爬取带有下拉框日期的数据?

本文介绍如何使用Python进行网页下拉日期选择数据的爬取。通过示例代码,演示了从特定网址抓取指定日期范围内的信息,并将结果保存到Excel文件中。主要涉及requests、BeautifulSoup库及日期处理。
摘要由CSDN通过智能技术生成

接上:

我大概用了这段代码试了下,程序既不报错也没有结果。请问错在哪?

import requests

import xlwt

from bs4 import BeautifulSoup

import datetime

import tqdm

def datelist(start, end):

start_date = datetime.datetime(*start)

end_date = datetime.datetime(*end)

result = []

curr_date = start_date

while curr_date != end_date:

result.append("%04d-%02d-%02d-%02d" % (curr_date.year, curr_date.month, curr_date.day,curr_date.hour))

return result

def get_html():

global h

s = requests.session()

url = 'http://www.szhec.gov.cn/pages/szepb/kqzl/TGzfwHjKqzlzs.jsp'

headers = {

'User-Agent':'Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0_1 like Mac OS X; ja-jp) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A306 Safari/6531.22.7',

'Referer':'http://www.szhec.gov.cn/pages/szepb/kqzl/TGzfwHjKqzlzs.jsp',

'Host':'www.szhec.gov.cn',

'Cookie':'JSESSIONID=1F7389E70413F613C3A166D8F5A963C4',

'Connection':'keep-alive',

'Accept-Language':'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',

'Accept-Encoding':'gzip, deflate',

'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'

}

r = s.post(url, data=dt, headers=headers)

h = r.content.decode('utf-8')

def get_excel():

book = xlwt.Workbook(encoding = 'utf-8',style_compression=0)

sheet = book.add_sheet('data',cell_overwrite_ok = True)

global dt

j = 0

for each in datelist((2016, 3, 1,0), (2016, 3, 31,23)):

dt = {'cdateEnd':each,'pageNo1':'1','pageNo2':''}

get_html()

soup = BeautifulSoup(h, "html.parser")

#j = 0

for tabb in soup.find_all('tr'):

i=0;

for tdd in tabb.find_all('td'):

#print (tdd.get_text()+",",)

sheet.write(j,i,tdd.get_text())

i = i+1

j=j+1

book.save(r'.\\re'+each+'.xls')

get_excel()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值