23.模拟登录cookies请求速询网站数据
采集速询网站数据:
网站地址:http://www.suxun0752.com/index.html
网站是需要账号登录才给返回信息的,我这里是直接拿的登录后的cookies请求的数据,cookies我也给了注释,没做深层的采集只是试采集了某一月份的。
简单分析一下 :
1.首先要先拿到cookies这样你才有权限去访问返回的数据。
2.分析页面翻页请求的参数,及需要筛选的标签年份、月份等。
把这几个参数综合起来去请求,就能完全获取页面数据了。我这里只是拿了一个月份的数据去请求获取数据。
问题:虽然有账号,但是采集到一定程度还是会被检测到访问过于频繁限制采集。
这里只是一个解决问题才去的简单方法。
采集状况,存入文本txt:
# coding:utf-8import requests import random,time import json #请求地址 target_url ='http://www.suxun0752.com/template/infoPrice/govlist.tdo?t=Fri%20Oct%2019%202018%2015:55:08%20GMT+0800%20(%E4%B8%AD%E5%9B%BD%E6%A0%87%E5%87%86%E6%97%B6%E9%97%B4)'headers = {'Accept': '*/*','Accept-Encoding': 'gzip, deflate','Accept-Language': 'zh-CN,zh;q=0.9','Connection': 'keep-alive','Content-Length': '75','Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8','Cookie': '************账号来之不易******************','Host': 'www.suxun0752.com','Origin': 'http://www.suxun0752.com','Referer': 'http://www.suxun0752.com/template/channelView.thtml?channel_id=00af','User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36','X-Requested-With': 'XMLHttpRequest', }
#模拟请求参数 for i in range(0,400):j=i*10form_data={'prov': '1','city': '12','area': '59','year': '17','month': '9','key': '','code': '','start': '{}'.format(j),'displayRecord':'10',}# 发送post请求,翻译数据response = requests.post(target_url, data=form_data, headers=headers)# print(response.text)content = json.loads(response.text)list = content['object']# print(list)for i in list:id = i['id']print(id)name = i['name']print(name)area = i['area']print(area)city = i['city']print(city)price = i['price']print(price)nicePrice = i['nicePrice']print(nicePrice)month = i['month']print(month)note = i['note']print(note)code = i['code']print(code)file_id = i['file_id']print(file_id)spec = i['spec']print(spec)unit = i['unit']print(unit)year = i['year']print(year)print('*'*100)time.sleep(random.randint(1,2))with open('text', 'a', encoding='utf-8')as f:f.write('\n'.join([str(id),str(name),str(area),str(price),str(nicePrice),str(month),str(code),str(file_id),spec,str(unit),str(year)]))f.write('\n' + '=' * 100 + '\n')
posted on 2018-11-07 17:57 五杀摇滚小拉夫 阅读(...) 评论(...) 编辑 收藏