Python模擬登錄實戰,采集整站表格數據
本節主要內容有:
- 通過requests庫模擬表單提交
- 通過pandas庫提取網頁表格
上周五,大師兄發給我一個網址,哭哭啼啼地求我:“去!把這個網頁上所有年所有縣所有作物的數據全爬下來,存到Access里!”
我看他可憐,勉為其難地揮揮手說:“好嘞,馬上就開始!”
目標分析
大師兄給我的網址是這個:https://www.ctic.org/crm?tdsourcetag=s_pctim_aiomsg
打開長這樣:

根據我學爬蟲并不久的經驗,通常只要把年月日之類的參數附加到url里面去,然后用requests.get拿到response解析html就完了,所以這次應該也差不多——除了要先想辦法獲得具體有哪些年份、地名、作物名稱,其他部分拿以前的代碼稍微改改就能用了,毫無挑戰性工作,生活真是太無聊了
點擊 View Summary 后出現目標網頁長這樣

那個大表格的數據就是目標數據了,好像沒什么了不起的——
有點不對勁
目標數據所在網頁的網址是這樣的:https://www.ctic.org/crm/?action=result ,剛剛選擇的那些參數并沒有作為url的參數啊!網址網頁都變了,所以也不是ajax
這和我想象的情況有巨大差別啊
嘗試獲取目標頁面
讓我來康康點擊View Summary這個按鈕時到底發生了啥:右鍵View Summary檢查是這樣:

實話說,這是我第一次遇到要提交表單的活兒。以前可能是上天眷顧我,統統get就能搞定,今天終于讓我碰上一個post了。
點擊View Summary,到DevTools里找network第一條:

不管三七二十一,post一下試試看
- import requests
- url = 'https://www.ctic.org/crm?tdsourcetag=s_pctim_aiomsg'
- headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
- 'AppleWebKit/537.36 (KHTML, like Gecko) '
- 'Chrome/74.0.3729.131 Safari/537.36',
- 'Host': 'www.ctic.org'}
- data = {'_csrf': 'SjFKLWxVVkkaSRBYQWYYCA1TMG8iYR8ReUYcSj04Jh4EBzIdBGwmLw==',
- 'CRMSearchForm[year]': '2011',
- 'CRMSearchForm[format]': 'Acres',
- 'CRMSearchForm[area]': 'County',
- 'CRMSearchForm[region]': 'Midwest',
- 'CRMSearchForm[state]': 'IL',
- 'CRMSearchForm[county]': 'Adams',
- 'CRMSearchForm[crop_type]': 'All',
- 'summary': 'county'}
- response = requests.post(url, data=data, headers=headers)
- print(response.status_code)
果不其然,輸出400……我猜這就是傳說中的cookies在搞鬼嗎?《Python3網絡爬蟲實戰》只看到第6章的我不禁有些心虛躍躍欲試呢!
首先,我搞不清cookies具體是啥,只知道它是用來維持會話的,應該來自于第一次get,搞出來看看先:
- response1 = requests.get(url, headers=headers)
- if response1.status_code == 200:
- cookies = response1.cookies
- print(cookies)
輸出:
- <RequestsCookieJar[<Cookie PHPSESSID=52asgghnqsntitqd7c8dqesgh6 for www.ctic.org/>, <Cookie _csrf=2571c72a4ca9699915ea4037b967827150715252de98ea2173b162fa376bad33s%3A32%3A%22TAhjwgNo5ElZzV55k3DMeFoc5TWrEmXj%22%3B for www.ctic.org/>]>
Nah,看不懂,不看不管,直接把它放到post里試試
- response2 = requests.post(url, data=data, headers=headers, cookies=cookies)
- print(response2.status_code)
還是400,氣氛突然變得有些焦灼,我給你cookies了啊,你還想要啥?!
突然,我發現一件事:post請求所帶的data中那個一開始就顯得很可疑的_csrf我仿佛在哪兒見過?
那個我完全看不懂的cookies里好像就有一個_csrf啊!但是兩個_csrf的值很明顯結構不一樣,試了一下把data里的_csrf換成cookies里的_csrf確實也不行。
但是我逐漸有了一個想法:這個兩個_csrf雖然不相等,但是應該是匹配的,我剛剛的data來自瀏覽器,cookies來自python程序,所以不匹配!
于是我又點開瀏覽器的DevTools,Ctrl+F搜索了一下,嘿嘿,發現了:

和

這三處。
第一處那里的下一行的csrf_token很明顯就是post請求所帶的data里的_csrf,另外兩個是js里的函數,雖然js沒好好學但也能看出來這倆是通過post請求獲得州名和縣名的,Binggo!一下子解決兩個問題。
為了驗證我的猜想,我打算先直接用requests獲取點擊View Summary前的頁面的HTML和cookies,將從HTML中提取的csrf_token值作為點擊View Summary時post請求的data里的_csrf值,同時附上cookies,這樣兩處_csrf就應該是匹配的了:
- from lxml import etree
- response1 = requests.get(url, headers=headers)
- cookies = response1.cookies
- html = etree.HTML(response1.text)
- csrf_token = html.xpath('/html/head/meta[3]/@content')[0]
- data.update({'_csrf': csrf_token})
- response2 = requests.post(url, data=data, headers=headers, cookies=cookies)
- print(response2.status_code)
輸出200,雖然和Chrome顯示的302不一樣,但是也表示成功,那就不管了。把response2.text寫入html文件打開看是這樣:

Yeah,數據都在!說明我的猜想是對的!那一會再試試我從沒用過的requests.Session()維持會話,自動處理cookies。
嘗試pandas庫提取網頁表格
現在既然已經拿到了目標頁面的HTML,那在獲取所有年、地區、州名、縣名之前,先測試一下pandas.read_html提取網頁表格的功能。
pandas.read_html這個函數時在寫代碼時IDE自動補全下拉列表里瞄到的,一直想試試來著,今天乘機拉出來溜溜:
- import pandas as pd
- df = pd.read_html(response2.text)[0]
- print(df)
輸出:

Yeah!拿到了,確實比自己手寫提取方便,而且數值字符串自動轉成數值,優秀!
準備所有參數
接下來要獲取所有年、地區、州名、縣名。年份和地區是寫死在HTML里的,直接xpath獲取:

州名、縣名根據之前發現的兩個js函數,要用post請求來獲得,其中州名要根據地區名獲取,縣名要根據州名獲取,套兩層循環就行
- def new():
- session = requests.Session()
- response = session.get(url=url, headers=headers)
- html = etree.HTML(response.text)
- return session, html
- session, html = new()
- years = html.xpath('//*[@id="crmsearchform-year"]/option/text()')
- regions = html.xpath('//*[@id="crmsearchform-region"]/option/text()')
- _csrf = html.xpath('/html/head/meta[3]/@content')[0]
- region_state = {}
- state_county = {}
- for region in regions:
- data = {'region': region, '_csrf': _csrf}
- response = session.post(url_state, data=data)
- html = etree.HTML(response.json())
- region_state[region] = {x: y for x, y in
- zip(html.xpath('//option/@value'),
- html.xpath('//option/text()'))}
- for state in region_state[region]:
- data = {'state': state, '_csrf': _csrf}
- response = session.post(url_county, data=data)
- html = etree.HTML(response.json())
- state_county[state] = html.xpath('//option/@value')
嘖嘖,使用requests.Session就完全不需要自己管理cookies了,方便!具體獲得的州名縣名就不放出來了,實在太多了。然后把所有年、地區、州名、縣名的可能組合先整理成csv文件,一會直接從csv里讀取并構造post請求的data字典:
- remain = [[str(year), str(region), str(state), str(county)]
- for year in years for region in regions
- for state in region_state[region] for county in state_county[state]]
- remain = pd.DataFrame(remain, columns=['CRMSearchForm[year]',
- 'CRMSearchForm[region]',
- 'CRMSearchForm[state]',
- 'CRMSearchForm[county]'])
- remain.to_csv('remain.csv', index=False)
- # 由于州名有縮寫和全稱,也本地保存一份
- import json
- with open('region_state.json', 'w') as json_file:
- json.dump(region_state, json_file, indent=4)
我看了一下,一共49473行——也就是說至少要發送49473個post請求才能爬完全部數據,純手工獲取的話大概要點擊十倍這個數字的次數……
正式開始
那么開始爬咯
- import pyodbc
- with open("region_state.json") as json_file:
- region_state = json.load(json_file)
- data = pd.read_csv('remain.csv')
- # 讀取已經爬取的
- cnxn = pyodbc.connect('DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};'
- 'DBQ=./ctic_crm.accdb')
- crsr = cnxn.cursor()
- crsr.execute('select Year_, Region, State, County from ctic_crm')
- done = crsr.fetchall()
- done = [list(x) for x in done]
- done = pd.DataFrame([list(x) for x in done], columns=['CRMSearchForm[year]',
- 'CRMSearchForm[region]',
- 'CRMSearchForm[state]',
- 'CRMSearchForm[county]'])
- done['CRMSearchForm[year]'] = done['CRMSearchForm[year]'].astype('int64')
- state2st = {y: x for z in region_state.values() for x, y in z.items()}
- done['CRMSearchForm[state]'] = [state2st[x]
- for x in done['CRMSearchForm[state]']]
- # 排除已經爬取的
- remain = data.append(done)
- remain = remain.drop_duplicates(keep=False)
- total = len(remain)
- print(f'{total} left.n')
- del data
- # %%
- remain['CRMSearchForm[year]'] = remain['CRMSearchForm[year]'].astype('str')
- columns = ['Crop',
- 'Total_Planted_Acres',
- 'Conservation_Tillage_No_Till',
- 'Conservation_Tillage_Ridge_Till',
- 'Conservation_Tillage_Mulch_Till',
- 'Conservation_Tillage_Total',
- 'Other_Tillage_Practices_Reduced_Till15_30_Residue',
- 'Other_Tillage_Practices_Conventional_Till0_15_Residue']
- fields = ['Year_', 'Units', 'Area', 'Region', 'State', 'County'] + columns
- data = {'CRMSearchForm[format]': 'Acres',
- 'CRMSearchForm[area]': 'County',
- 'CRMSearchForm[crop_type]': 'All',
- 'summary': 'county'}
- headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
- 'AppleWebKit/537.36 (KHTML, like Gecko) '
- 'Chrome/74.0.3729.131 Safari/537.36',
- 'Host': 'www.ctic.org',
- 'Upgrade-Insecure-Requests': '1',
- 'DNT': '1',
- 'Connection': 'keep-alive'}
- url = 'https://www.ctic.org/crm?tdsourcetag=s_pctim_aiomsg'
- headers2 = headers.copy()
- headers2 = headers2.update({'Referer': url,
- 'Origin': 'https://www.ctic.org'})
- def new():
- session = requests.Session()
- response = session.get(url=url, headers=headers)
- html = etree.HTML(response.text)
- _csrf = html.xpath('/html/head/meta[3]/@content')[0]
- return session, _csrf
- session, _csrf = new()
- for _, row in remain.iterrows():
- temp = dict(row)
- data.update(temp)
- data.update({'_csrf': _csrf})
- while True:
- try:
- response = session.post(url, data=data, headers=headers2, timeout=15)
- break
- except Exception as e:
- session.close()
- print(e)
- print('nSleep 30s.n')
- time.sleep(30)
- session, _csrf = new()
- data.update({'_csrf': _csrf})
- df = pd.read_html(response.text)[0].dropna(how='all')
- df.columns = columns
- df['Year_'] = int(temp['CRMSearchForm[year]'])
- df['Units'] = 'Acres'
- df['Area'] = 'County'
- df['Region'] = temp['CRMSearchForm[region]']
- df['State'] = region_state[temp['CRMSearchForm[region]']][temp['CRMSearchForm[state]']]
- df['County'] = temp['CRMSearchForm[county]']
- df = df.reindex(columns=fields)
- for record in df.itertuples(index=False):
- tuple_record = tuple(record)
- sql_insert = f'INSERT INTO ctic_crm VALUES {tuple_record}'
- sql_insert = sql_insert.replace(', nan,', ', null,')
- crsr.execute(sql_insert)
- crsr.commit()
- print(total, row.to_list())
- total -= 1
- else:
- print('Done!')
- crsr.close()
- cnxn.close()
注意中間有個try...except..語句,是因為不定時會發生Connection aborted的錯誤,有時9000次才斷一次,有時一次就斷,這也是我加上了讀取已經爬取的和排除已經爬取的原因,而且擔心被識別出爬蟲,把headers寫的豐富了一些(好像并沒有什么卵用),并且每次斷開都暫停個30s并重新開一個會話

然后把程序開著過了一個周末,命令行里終于打出了Done!,到Access里一看有816288條記錄,心想:下次試試多線程(進程)和代理池。
周一,我把跑出來的數據發給大師兄,大師兄回我:“好的”。
隔著屏幕我都能感受到滔滔不絕的敬仰和感激之情,一直到現在,大師兄都感動地說不出話來。




























