- Anaconda是一個集成環境(基於機器學習和數據分析的開發環境)
- 基於瀏覽器的一種可視化開發工具:jupyter notebook
- 可以在指定目錄的終端中錄入jupyter notebook指令,然后啟動服務。
- cell是分為不同模式的:
- Code:編寫python代碼
- markDown:編寫筆記
- 快捷鍵:
- 添加cell:a,b
- 刪除cell:x
- 執行:shift+enter
- tab:
- 切換cell的模式:
- m ==>markdowm模式
- y ==》代碼模式
- 打開幫助文檔:shift+tab
環境變量配置
在某個文件夾下shift+右鍵 打開powershell 輸入jupyter notebook打開 web ,即當前目錄是web 的根目錄
-
-
規避風險:
-
嚴格遵守網站設置的robots協議;
-
在規避反爬蟲措施的同時,需要優化自己的代碼,避免干擾被訪問網站的正常運行;
-
在使用、傳播抓取到的信息時,應審查所抓取的內容,如發現屬於用戶的個人信息、隱私或者他人的商業秘密的,應及時停止並刪除。
-
-
-
robots協議:文本協議
-
- www.xx.com/robots.txt 查看
- 特性:防君子不防小人的文本協議
二、requtest
- 什么是requests模塊?
- Python中封裝好的一個基於網絡請求的模塊。
- requests模塊的作用?
- 用來模擬瀏覽器發請求
- requests模塊的環境安裝:
- pip install requests
- requests模塊的編碼流程:
- 1.指定url
- 2.發起請求
- 3.獲取響應數據
- 4.持久化存儲

import requests #1.指定url url = 'https://www.sogou.com/' #2.請求發送get:get返回值是一個響應對象 response = requests.get(url=url) #3.獲取響應數據 page_text = response.text #返回的是字符串形式的響應數據 #4.持久化存儲 with open('sogou.html','w',encoding='utf-8') as fp: fp.write(page_text)

#需要讓url攜帶的參數動態化 url = 'https://www.sogou.com/web' #實現參數動態化 wd = input('enter a key:') params = { 'query':wd } #在請求中需要將請求參數對應的字典作用到params這個get方法的參數中 response = requests.get(url=url,params=params) page_text = response.text fileName = wd+'.html' with open(fileName,'w',encoding='utf-8') as fp: fp.write(page_text)
- 上述代碼執行后發現:
- 1.出現了亂碼
- 2.數據量級不對

url = 'https://www.sogou.com/web' #實現參數動態化 wd = input('enter a key:') params = { 'query':wd } #在請求中需要將請求參數對應的字典作用到params這個get方法的參數中 response = requests.get(url=url,params=params) response.encoding = 'utf-8' #修改響應數據的編碼格式 page_text = response.text fileName = wd+'.html' with open(fileName,'w',encoding='utf-8') as fp: fp.write(page_text)
- UA檢測:門戶網站通過檢測請求載體的身份標識判定改請求是否為爬蟲發起的請求
- UA偽裝:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36
- user-agent

url = 'https://www.sogou.com/web' #實現參數動態化 wd = input('enter a key:') params = { 'query':wd } headers = { 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36' } #在請求中需要將請求參數對應的字典作用到params這個get方法的參數中 response = requests.get(url=url,params=params,headers=headers) response.encoding = 'utf-8' #修改響應數據的編碼格式 page_text = response.text fileName = wd+'.html' with open(fileName,'w',encoding='utf-8') as fp: fp.write(page_text)
三 ajax
#爬取的是豆瓣電影中電影的詳情數據
https://movie.douban.com/typerank?type_name=%E7%88%B1%E6%83%85&type=13&interval_id=100:90&action=
#分析:
當滾動條被滑動到頁面底部的時候,當前頁面發生了局部刷新(ajax的請求)
動態加載的頁面數據
- 是通過另一個單獨的請求請求到的數據

url = 'https://movie.douban.com/j/chart/top_list' start = input('您想從第幾部電影開始獲取:') limit = input('您想獲取多少電影數據:') # url 中的參數 dic = { 'type': '13', 'interval_id': '100:90', 'action': '', 'start': start, 'limit': limit, } response = requests.get(url=url,params=dic,headers=headers) page_text = response.json() #json()返回的是序列化好的實例對象 for dic in page_text: print(dic['title']+':'+dic['score'])

#肯德基餐廳查詢http://www.kfc.com.cn/kfccda/storelist/index.aspx url = 'http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=keyword' for page in range(1,5): data = { 'cname': '', 'pid': '', 'keyword': '西安', 'pageIndex': str(page), 'pageSize': '10', } response = requests.post(url=url,headers=headers,data=data) print(response.json())
如何檢測頁面中是否存在動態加載的數據?
- 基於抓包工具實現
- 先捕獲網站請求后所有的數據包
- 在數據包中定位到地址欄所對應請求的數據包,在response選項卡對應的數據中進行局部搜索(頁面中的某一組內容)
- 可以搜索到:爬取的數據不是動態加載的
- 沒有搜索到:爬取的數據是動態加載的
- 如何定位動態加載的數據在哪個數據包中呢?
- 進行全局搜索