1. 程式人生 > >23.模擬登入cookies請求速詢網站資料

23.模擬登入cookies請求速詢網站資料

採集速詢網站資料:

網站地址:http://www.suxun0752.com/index.html

網站是需要賬號登入才給返回資訊的,我這裡是直接拿的登入後的cookies請求的資料,cookies我也給了註釋,沒做深層的採集只是試採集了某一月份的。

簡單分析一下 :

1.首先要先拿到cookies這樣你才有許可權去訪問返回的資料。
2.分析頁面翻頁請求的引數,及需要篩選的標籤年份、月份等。
把這幾個引數綜合起來去請求,就能完全獲取頁面資料了。我這裡只是拿了一個月份的資料去請求獲取資料。

問題:雖然有賬號,但是採集到一定程度還是會被檢測到訪問過於頻繁限制採集。
這裡只是一個解決問題才去的簡單方法。

採集狀況,存入文字txt:

 

 
 
 
 
# coding:utf-8

import 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*10 form_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')