Python?Ajax爬蟲案例分享
1. 抓取街拍圖片
2. 分析街拍圖片結(jié)構(gòu)
keyword: 街拍 pd: atlas dvpf: pc aid: 4916 page_num: 1 search_json: {"from_search_id":"20220104115420010212192151532E8188","origin_keyword":"街拍","image_keyword":"街拍"} rawJSON: 1 search_id: 202201041159040101501341671A4749C4
可以找到規(guī)律,page_num從1
開始累加,其他參數(shù)不變
3. 按功能不同編寫不同方法組織代碼
3.1 獲取網(wǎng)頁json格式數(shù)據(jù)
def get_page(page_num): ? ? global headers ? ? headers = { ? ? ? ? 'Host': 'so.toutiao.com', ? ? ? ? #'Referer': 'https://so.toutiao.com/search?keyword=%E8%A1%97%E6%8B%8D&pd=atlas&dvpf=pc&aid=4916&page_num=0&search_json={%22from_search_id%22:%22202112272022060101510440283EE83D67%22,%22origin_keyword%22:%22%E8%A1%97%E6%8B%8D%22,%22image_keyword%22:%22%E8%A1%97%E6%8B%8D%22}', ? ? ? ? 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36', ? ? ? ? 'X-Requested-With': 'XMLHttpRequest', ? ? ? ? 'Cookie': 'msToken=S0DFBkZ9hmyLOGYd3_QjhhXgrm38qTyOITnkNb0t_oavfbVxuYV1JZ0tT5hLgswSfmZLFD6c2lONm_5TomUQXVXjen7CIxM2AGwbhHRYKjhg; _S_DPR=1.5; _S_IPAD=0; MONITOR_WEB_ID=7046351002275317255; ttwid=1%7C0YdWalNdIiSpIk3CvvHwV25U8drq3QAj08E8QOApXhs%7C1640607595%7C720e971d353416921df127996ed708931b4ae28a0a8691a5466347697e581ce8; _S_WIN_WH=262_623' ? ? } ? ? params = { ? ? ? ? 'keyword': '街拍', ? ? ? ? 'pd': 'atlas', ? ? ? ? 'dvpf': 'pc', ? ? ? ? 'aid': '4916', ? ? ? ? 'page_num': page_num, ? ? ? ? 'search_json': '%7B%22from_search_id%22%3A%22202112272022060101510440283EE83D67%22%2C%22origin_keyword%22%3A%22%E8%A1%97%E6%8B%8D%22%2C%22image_keyword%22%3A%22%E8%A1%97%E6%8B%8D%22%7D', ? ? ? ? 'rawJSON': 1, ? ? ? ? 'search_id': '2021122721183101015104402851E3883D' ? ? } ? ? url = 'https://so.toutiao.com/search?' + urlencode(params) ? ? print(url) ? ? try: ? ? ? ? response=requests.get(url,headers=headers,params=params) ? ? ? ? if response.status_code == 200: ? ? ? ? #if response.content: ? ? ? ? ? ? #print(response.json()) ? ? ? ? ? ? return response.json() ? ? except requests.ConnectionError: ? ? ? ? return None
3.2 從json格式數(shù)據(jù)提取街拍圖片
def get_images(json): ? ? images = json.get('rawData').get('data') ? ? for image in images: ? ? ? ? link = image.get('img_url') ? ? ? ? yield link
3.3 將街拍圖片以其md5碼命名并保存圖片
實(shí)現(xiàn)一個(gè)保存圖片的方法save_image()
,其中 item 就是前面 get_images() 方法返回的一個(gè)字典。在該方法中,首先根據(jù) item
的 title 來創(chuàng)建文件夾,然后請(qǐng)求這個(gè)圖片鏈接,獲取圖片的二進(jìn)制數(shù)據(jù),以二進(jìn)制的形式寫入文件。圖片的名稱可以使用其內(nèi)容的 MD5 值,這樣可以去除重復(fù)。相關(guān)
代碼如下:
def save_image(link): ? ? data = requests.get(link).content ? ? with open(f'./image/{md5(data).hexdigest()}.jpg', 'wb')as f:#使用data的md5碼作為圖片名 ? ? ? ? f.write(data)
3.4 main()調(diào)用其他函數(shù)
def main(page_num): ? ? json = get_page(page_num) ? ? for link in get_images(json): ? ? ? ? #print(link) ? ? ? ? save_image(link)
4 抓取20page今日頭條街拍圖片數(shù)據(jù)
這里定義了分頁的起始頁數(shù)和終止頁數(shù),分別為GROUP_START
和 GROUP_END
,還利用了多線程的線程池,調(diào)用其 map() 方法實(shí)現(xiàn)程下載。
if __name__ == '__main__': ? ? GROUP_START = 1 ? ? GROUP_END = 20 ? ? pool = Pool() ? ? groups = ([x for x in range(GROUP_START, GROUP_END + 1)]) ? ? #print(groups) ? ? pool.map(main, groups) ? ? pool.close() ? ? pool.join()
import requests from urllib.parse import urlencode from hashlib import md5 from multiprocessing.pool import Pool def get_page(page_num): ? ? global headers ? ? headers = { ? ? ? ? 'Host': 'so.toutiao.com', ? ? ? ? #'Referer': 'https://so.toutiao.com/search?keyword=%E8%A1%97%E6%8B%8D&pd=atlas&dvpf=pc&aid=4916&page_num=0&search_json={%22from_search_id%22:%22202112272022060101510440283EE83D67%22,%22origin_keyword%22:%22%E8%A1%97%E6%8B%8D%22,%22image_keyword%22:%22%E8%A1%97%E6%8B%8D%22}', ? ? ? ? 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36', ? ? ? ? 'X-Requested-With': 'XMLHttpRequest', ? ? ? ? 'Cookie': 'msToken=S0DFBkZ9hmyLOGYd3_QjhhXgrm38qTyOITnkNb0t_oavfbVxuYV1JZ0tT5hLgswSfmZLFD6c2lONm_5TomUQXVXjen7CIxM2AGwbhHRYKjhg; _S_DPR=1.5; _S_IPAD=0; MONITOR_WEB_ID=7046351002275317255; ttwid=1%7C0YdWalNdIiSpIk3CvvHwV25U8drq3QAj08E8QOApXhs%7C1640607595%7C720e971d353416921df127996ed708931b4ae28a0a8691a5466347697e581ce8; _S_WIN_WH=262_623' ? ? } ? ? params = { ? ? ? ? 'keyword': '街拍', ? ? ? ? 'pd': 'atlas', ? ? ? ? 'dvpf': 'pc', ? ? ? ? 'aid': '4916', ? ? ? ? 'page_num': page_num, ? ? ? ? 'search_json': '%7B%22from_search_id%22%3A%22202112272022060101510440283EE83D67%22%2C%22origin_keyword%22%3A%22%E8%A1%97%E6%8B%8D%22%2C%22image_keyword%22%3A%22%E8%A1%97%E6%8B%8D%22%7D', ? ? ? ? 'rawJSON': 1, ? ? ? ? 'search_id': '2021122721183101015104402851E3883D' ? ? } ? ? url = 'https://so.toutiao.com/search?' + urlencode(params) ? ? print(url) ? ? try: ? ? ? ? response=requests.get(url,headers=headers,params=params) ? ? ? ? if response.status_code == 200: ? ? ? ? #if response.content: ? ? ? ? ? ? #print(response.json()) ? ? ? ? ? ? return response.json() ? ? except requests.ConnectionError: ? ? ? ? return None def get_images(json): ? ? images = json.get('rawData').get('data') ? ? for image in images: ? ? ? ? link = image.get('img_url') ? ? ? ? yield link def save_image(link): ? ? data = requests.get(link).content ? ? with open(f'./image/{md5(data).hexdigest()}.jpg', 'wb')as f:#使用data的md5碼作為圖片名 ? ? ? ? f.write(data) def main(page_num): ? ? json = get_page(page_num) ? ? for link in get_images(json): ? ? ? ? #print(link) ? ? ? ? save_image(link) if __name__ == '__main__': ? ? GROUP_START = 1 ? ? GROUP_END = 20 ? ? pool = Pool() ? ? groups = ([x for x in range(GROUP_START, GROUP_END + 1)]) ? ? #print(groups) ? ? pool.map(main, groups) ? ? pool.close() ? ? pool.join()
到此這篇關(guān)于Python Ajax爬蟲案例分享的文章就介紹到這了,更多相關(guān)Python Ajax爬蟲內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python7個(gè)爬蟲小案例詳解(附源碼)下篇
- Python7個(gè)爬蟲小案例詳解(附源碼)中篇
- Python7個(gè)爬蟲小案例詳解(附源碼)上篇
- 利用Python爬蟲爬取金融期貨數(shù)據(jù)的案例分析
- Python爬蟲采集Tripadvisor數(shù)據(jù)案例實(shí)現(xiàn)
- Python爬蟲入門案例之爬取去哪兒旅游景點(diǎn)攻略以及可視化分析
- Python爬蟲入門案例之爬取二手房源數(shù)據(jù)
- Python爬蟲入門案例之回車桌面壁紙網(wǎng)美女圖片采集
- Python爬蟲之Scrapy環(huán)境搭建案例教程
- 用Python爬蟲破解滑動(dòng)驗(yàn)證碼的案例解析
- python爬蟲系列網(wǎng)絡(luò)請(qǐng)求案例詳解
- python爬蟲破解字體加密案例詳解
- python爬蟲線程池案例詳解(梨視頻短視頻爬取)
- python爬蟲scrapy框架的梨視頻案例解析
- python爬蟲利器之requests庫的用法(超全面的爬取網(wǎng)頁案例)
- Python爬蟲實(shí)戰(zhàn)案例之爬取喜馬拉雅音頻數(shù)據(jù)詳解
- Python爬蟲Scrapy框架CrawlSpider原理及使用案例
- Python爬蟲之對(duì)CSDN榜單進(jìn)行分析
相關(guān)文章
pytorch-神經(jīng)網(wǎng)絡(luò)擬合曲線實(shí)例
今天小編就為大家分享一篇pytorch-神經(jīng)網(wǎng)絡(luò)擬合曲線實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-01-01Python數(shù)據(jù)分析中Groupby用法之通過字典或Series進(jìn)行分組的實(shí)例
下面小編就為大家分享一篇Python數(shù)據(jù)分析中Groupby用法之通過字典或Series進(jìn)行分組的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-12-12基于Python實(shí)現(xiàn)批量讀取大量nc格式文件并導(dǎo)出全部時(shí)間信息
這篇文章主要為大家詳細(xì)介紹了如何基于Python語言,逐一讀取大量.nc格式的多時(shí)相柵格文件并導(dǎo)出其中所具有的全部時(shí)間信息的方法,需要的可以參考下2024-01-01使用pandas的DataFrame的plot方法繪制圖像的實(shí)例
今天小編就為大家分享一篇使用pandas的DataFrame的plot方法繪制圖像的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-05-05PyQt5的安裝配置過程,將ui文件轉(zhuǎn)為py文件后顯示窗口的實(shí)例
今天小編就為大家分享一篇PyQt5的安裝配置過程,將ui文件轉(zhuǎn)為py文件后顯示窗口的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-06-06Python遠(yuǎn)程linux執(zhí)行命令實(shí)現(xiàn)
這篇文章主要介紹了Python遠(yuǎn)程linux執(zhí)行命令實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11Python利用itchat對(duì)微信中好友數(shù)據(jù)實(shí)現(xiàn)簡單分析的方法
Python 熱度一直很高,我感覺這就是得益于擁有大量的包資源,極大的方便了開發(fā)人員的需求。下面這篇文章主要給大家介紹了關(guān)于Python利用itchat實(shí)現(xiàn)對(duì)微信中好友數(shù)據(jù)進(jìn)行簡單分析的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。2017-11-11python FastApi實(shí)現(xiàn)數(shù)據(jù)表遷移流程詳解
今天我們來聊一聊在FastApi里面,數(shù)據(jù)遷移工作,F(xiàn)astAPI是一個(gè)現(xiàn)代的,快速(高性能)python web框架。本文將利用fastapi實(shí)現(xiàn)數(shù)據(jù)表遷移功能,文中的示例代碼講解詳細(xì),需要的可以參考一下2022-08-08如何利用Python實(shí)現(xiàn)簡易的音頻播放器
這篇文章主要介紹了如何利用Python實(shí)現(xiàn)簡易的音頻播放器,需要用到的庫有pygame和tkinter,實(shí)現(xiàn)音頻播放的功能,供大家學(xué)習(xí)參考,希望對(duì)你有所幫助2022-03-03