您好,登錄后才能下訂單哦!
這篇文章主要講解了“怎么用Python采集整站表格數(shù)據(jù)”,文中的講解內(nèi)容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“怎么用Python采集整站表格數(shù)據(jù)”吧!
目標分析
大師兄給我的網(wǎng)址是這個:https://www.ctic.org/crm?tdsourcetag=s_pctim_aiomsg
打開長這樣:
根據(jù)我學爬蟲并不久的經(jīng)驗,通常只要把年月日之類的參數(shù)附加到url里面去,然后用requests.get拿到response解析html就完了,所以這次應(yīng)該也差不多——除了要先想辦法獲得具體有哪些年份、地名、作物名稱,其他部分拿以前的代碼稍微改改就能用了,毫無挑戰(zhàn)性工作,生活真是太無聊了
點擊 View Summary 后出現(xiàn)目標網(wǎng)頁長這樣
那個大表格的數(shù)據(jù)就是目標數(shù)據(jù)了,好像沒什么了不起的——
有點不對勁
目標數(shù)據(jù)所在網(wǎng)頁的網(wǎng)址是這樣的:https://www.ctic.org/crm/?action=result ,剛剛選擇的那些參數(shù)并沒有作為url的參數(shù)啊!網(wǎng)址網(wǎng)頁都變了,所以也不是ajax
這和我想象的情況有巨大差別啊
嘗試獲取目標頁面
讓我來康康點擊View Summary這個按鈕時到底發(fā)生了啥:右鍵View Summary檢查是這樣:
實話說,這是我第一次遇到要提交表單的活兒。以前可能是上天眷顧我,統(tǒng)統(tǒng)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': 'SjFKLWxVVkkaSRBYQWYYCA1TMG8iYR8ReUYcSj04Jh5EBzIdBGwmLw==', '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網(wǎng)絡(luò)爬蟲實戰(zhàn)》只看到第6章的我不禁有些心虛躍躍欲試呢!
首先,我搞不清cookies具體是啥,只知道它是用來維持會話的,應(yīng)該來自于第一次get,搞出來看看先:
response1 = requests.get(url, headers=headers) if response1.status_code == 200: cookies = response1.cookies print(cookies)
輸出:
<RequestsCookieJar[<Cookie PHPSESSID=52asgghnqsntitqd7c8dqesgh7 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了啊,你還想要啥?!
突然,我發(fā)現(xiàn)一件事:post請求所帶的data中那個一開始就顯得很可疑的_csrf我仿佛在哪兒見過?
那個我完全看不懂的cookies里好像就有一個_csrf啊!但是兩個_csrf的值很明顯結(jié)構(gòu)不一樣,試了一下把data里的_csrf換成cookies里的_csrf確實也不行。
但是我逐漸有了一個想法:這個兩個_csrf雖然不相等,但是應(yīng)該是匹配的,我剛剛的data來自瀏覽器,cookies來自python程序,所以不匹配!
于是我又點開瀏覽器的DevTools,Ctrl+F搜索了一下,嘿嘿,發(fā)現(xiàn)了:
和
這三處。
第一處那里的下一行的csrf_token很明顯就是post請求所帶的data里的_csrf,另外兩個是js里的函數(shù),雖然js沒好好學但也能看出來這倆是通過post請求獲得州名和縣名的,Binggo!一下子解決兩個問題。
為了驗證我的猜想,我打算先直接用requests獲取點擊View Summary前的頁面的HTML和cookies,將從HTML中提取的csrf_token值作為點擊View Summary時post請求的data里的_csrf值,同時附上cookies,這樣兩處_csrf就應(yīng)該是匹配的了:
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,數(shù)據(jù)都在!說明我的猜想是對的!那一會再試試我從沒用過的requests.Session()維持會話,自動處理cookies。
嘗試pandas庫提取網(wǎng)頁表格
現(xiàn)在既然已經(jīng)拿到了目標頁面的HTML,那在獲取所有年、地區(qū)、州名、縣名之前,先測試一下pandas.read_html提取網(wǎng)頁表格的功能。
pandas.read_html這個函數(shù)時在寫代碼時IDE自動補全下拉列表里瞄到的,一直想試試來著,今天乘機拉出來溜溜:
import pandas as pd df = pd.read_html(response2.text)[0] print(df)
輸出:
Yeah!拿到了,確實比自己手寫提取方便,而且數(shù)值字符串自動轉(zhuǎn)成數(shù)值,優(yōu)秀!
準備所有參數(shù)
接下來要獲取所有年、地區(qū)、州名、縣名。年份和地區(qū)是寫死在HTML里的,直接xpath獲?。?/p>
州名、縣名根據(jù)之前發(fā)現(xiàn)的兩個js函數(shù),要用post請求來獲得,其中州名要根據(jù)地區(qū)名獲取,縣名要根據(jù)州名獲取,套兩層循環(huán)就行
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了,方便!具體獲得的州名縣名就不放出來了,實在太多了。然后把所有年、地區(qū)、州名、縣名的可能組合先整理成csv文件,一會直接從csv里讀取并構(gòu)造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行——也就是說至少要發(fā)送49473個post請求才能爬完全部數(shù)據(jù),純手工獲取的話大概要點擊十倍這個數(shù)字的次數(shù)……
正式開始
那么開始爬咯
import pyodbc with open("region_state.json") as json_file: region_state = json.load(json_file) data = pd.read_csv('remain.csv') # 讀取已經(jīng)爬取的 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]']] # 排除已經(jīng)爬取的 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..語句,是因為不定時會發(fā)生Connection aborted的錯誤,有時9000次才斷一次,有時一次就斷,這也是我加上了讀取已經(jīng)爬取的和排除已經(jīng)爬取的原因,而且擔心被識別出爬蟲,把headers寫的豐富了一些(好像并沒有什么卵用),并且每次斷開都暫停個30s并重新開一個會話
然后把程序開著過了一個周末,命令行里終于打出了Done!,到Access里一看有816288條記錄,心想:下次試試多線程(進程)和代理池。
感謝各位的閱讀,以上就是“怎么用Python采集整站表格數(shù)據(jù)”的內(nèi)容了,經(jīng)過本文的學習后,相信大家對怎么用Python采集整站表格數(shù)據(jù)這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。