爬取今日頭條Ajax請(qǐng)求
網(wǎng)址:https://www.toutiao.com/
搜索頭條
可以得到這個(gè)網(wǎng)址:
https://www.toutiao.com/search/?keyword=%E8%A1%97%E6%8B%8D
開發(fā)者工具查看:
我們?cè)谒阉髦胁](méi)有發(fā)現(xiàn)上面的文字,那么我們可以初步判定,這個(gè)由Ajax加載,然后渲染出來(lái)的。此時(shí)切換到xhr過(guò)濾,可以看到確實(shí)是ajax請(qǐng)求。
觀察請(qǐng)求的特點(diǎn),發(fā)現(xiàn)只有offset是改變的,而且一次加20,。
我們可以用它來(lái)控制數(shù)據(jù)分頁(yè),然后把圖片下載下來(lái)。代碼如下:
import requests import os from urllib.parse import urlencode from hashlib import md5 from multiprocessing.pool import Pool from requests import codes def get_page(offset): params = { "offset":offset, "format":"json", "keyword":"街拍", "autoload":"true", "count":"20", "cur_tab":"1", "from":"search_tab" } url = 'https://www.toutiao.com/search_content/?'+urlencode(params) try: response = requests.get(url) if response.status_code == 200: # print(url) return response.json() except requests.ConnectionError: return None # get_page(0) def get_images(json): if json.get('data'): for item in json.get('data'): if item.get('cell_type') is not None: continue title = item.get('title') images = item.get('image_list') for image in images: yield { 'title':title, 'image':'https:' + image.get('url'), } def save_image(item): #os.path.sep 路徑分隔符‘//' img_path = 'img' + os.path.sep + item.get('title') if not os.path.exists(img_path): os.makedirs(img_path) try: resp = requests.get(item.get('image')) # print(type(resp)) if codes.ok == resp.status_code: file_path = img_path + os.path.sep + '{file_name}.{file_suffix}'.format( file_name=md5(resp.content).hexdigest(),#md5是一種加密算法獲取圖片的二進(jìn)制數(shù)據(jù),以二進(jìn)制形式寫入文件 file_suffix='jpg') if not os.path.exists(file_path): with open(file_path,'wb')as f: f.write(resp.content) print('Downladed image path is %s' % file_path) else: print('Already Downloaded',file_path) except requests.ConnectionError: print('Failed to Save Image,item %s' % item) def main(offset): json = get_page(offset) for item in get_images(json): print(item) save_image(item) GROUP = 0 GROUP_END = 2 if __name__ == '__main__': pool = Pool() groups = ([x*20 for x in range(GROUP,GROUP_END)]) pool.map(main,groups) #將groups一個(gè)個(gè)調(diào)出來(lái)傳給main函數(shù) pool.close() pool.join() #保證子進(jìn)程結(jié)束后再向下執(zhí)行 pool.join(1) 等待一秒
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)我們的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
上一篇:layui Ajax請(qǐng)求給下拉框賦值的實(shí)例
欄 目:AJAX相關(guān)
下一篇:ajax動(dòng)態(tài)加載json數(shù)據(jù)并詳細(xì)解析
本文標(biāo)題:爬取今日頭條Ajax請(qǐng)求
本文地址:http://mengdiqiu.com.cn/a1/AJAXxiangguan/11289.html
您可能感興趣的文章


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-11layui的checbox在Ajax局部刷新下的設(shè)置方
- 01-11ajax請(qǐng)求后臺(tái)得到j(luò)son數(shù)據(jù)后動(dòng)態(tài)生成樹
- 01-11ajax獲得json對(duì)象數(shù)組 循環(huán)輸出數(shù)據(jù)的
- 01-11解決ajax請(qǐng)求后臺(tái),有時(shí)收不到返回值的
- 01-11詳談ajax返回?cái)?shù)據(jù)成功 卻進(jìn)入error的方
- 01-11解決AJAX返回狀態(tài)200沒(méi)有調(diào)用success的問(wèn)
- 01-11快速解決ajax返回值給外部函數(shù)的問(wèn)題
- 01-11Ajax實(shí)現(xiàn)動(dòng)態(tài)顯示并操作表信息的方法
- 01-11ajax實(shí)現(xiàn)從后臺(tái)拿數(shù)據(jù)顯示在HTML前端的
- 01-11ajax動(dòng)態(tài)查詢數(shù)據(jù)庫(kù)數(shù)據(jù)并顯示在前臺(tái)
隨機(jī)閱讀
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10C#中split用法實(shí)例總結(jié)
- 04-02jquery與jsp,用jquery
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10delphi制作wav文件的方法