爬蟲第一天


爬蟲學習參考博客:https://www.cnblogs.com/cyycyhcbw/articles/10442399.html

1.使用軟件anaconda

2.輸入jupyter notbook 

3.anaconda使用快捷鍵

-插入cell:     a  上一行插入        b 下一行插入

-刪除: x

-模式切換: m切換到master      y 切換到code

-執行cell:shift+enter

-tab

-打開幫助文檔:shift+tab

4.爬蟲概述

通過編寫程序模擬瀏覽器上網,然后讓其去互聯網上爬取數據的過程

5.爬蟲的分類:

  5.1通用爬蟲:爬取一整張頁面源碼數據

  5.2聚焦爬蟲:爬取頁面中指定的局部數據

  5.3增量式爬蟲:檢測網站數據更新的情況,爬取的就是網站中最新的數據

6.反爬機制

7.反反爬策略

8.第一個反爬機制:robots.txt協議

二:requests模塊的應用

-requests:功能強大,操作簡單

-urllib

-作用:用來模擬瀏覽器發請求

-編碼流程

  -指定url

  -發起請求:requests.get/post

  -獲取響應數據

  -持久化存儲

#爬取搜狗首頁的源碼數據
import requests

#1.
url="https://www.sogou.com/"
#2.
response=requestes.get(url=url)
#3.
page_text=response.text
#4.
with open('/sougou.html','w',encoding='utf-8') as fp:
    fp.write(page_text)

 

 

 

#簡易的網頁采集器
wd=input('enter a word')
#1.
url="https://www.sogou.com/web?"
#2.將請求參數設定為動態的
param={
    "query":wd
}
#3.params傳參
response=requests.get(url=url,params=param)
#4.手動設置響應數據的編碼,處理中文亂碼問題
response.encoding='utf-8'
#5.text返回的是字符串形式的響應數據
page_text=response.text

filename=wd+'.html'

with open(filename,'w',enconding='utf-8') as fp:
    fp.write(page_text)


#打開頁面會出現以下錯誤,這是網站的反爬機制
#用戶您好,我們的系統檢測到您網絡中存在異常訪問請求。
#此驗證碼用於確認這些請求是您的正常行為而不是自動程序發出的,需要您協助驗證。

 

 User-Agent:請求載體的身份標識

UA檢測:門戶網站的服務器端會檢測每一個請求的UA,如果檢測到請求的UA為爬蟲程序,則請求失敗

UA偽裝:

 

#簡易的網頁采集器
wd=input('enter a word')
#1.
url="https://www.sogou.com/web?"
#2.將請求參數設定為動態的
param={
    "query":wd
}
#UA偽裝
headers={
    "User-Agent":'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
}
#3.params傳參
response=requests.get(url=url,params=param,headers=headers)
#4.手動設置響應數據的編碼,處理中文亂碼問題
response.encoding='utf-8'
#5.text返回的是字符串形式的響應數據
page_text=response.text

filename=wd+'.html'

with open(filename,'w',encoding='utf-8') as fp:
    fp.write(page_text)

 

 

 

 

#爬取肯德基餐廳位置信息(有關於ajax刷新頁面的數據采集)
url='http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=keyword'
#動態生成城市
city=input('enter a city name:')
data={
    'cname': '',
    'pid': '',
    'keyword': city,
    'pageIndex': '1',
    'pageSize': '10',
}
headers={
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
}
response=requests.post(url=url,data=data,headers=headers)

page_text=response.json()
print(page_text)

#這里文件操作可以覆蓋寫入,也可以追加寫入

fp=open('/kfc.text','w',encoding='utf-8')


for dic in page_text['Table1']:
    addr=dic['addressDetail']
print(addr)
    fp.write(addr+'\n')
fp.clouse()
  •  爬取動態加載的頁面數據

 

#爬取豆瓣電影的電影詳情數據
url='https://movie.douban.com/j/chart/top_list'
s =1
limit=100
param={
    'type': '5'
    'interval_id': '100:90'
    'action':'' 
    'start': s
    'limit': limit
}
response=requests.get(url=url,headers=headers,params=param)

page_text=response.json()

 爬取葯監局相關的數據http://125.35.6.84:81/xk/

  • 發現首頁中的所有的企業數據都是動態加載出來的
  • 通過抓包工具捕獲動態加載數據對應的數據包
  • 從上一步的數據包對應的響應數據中提取到企業的相關信息(ID)
  • 通過分析每一家企業詳情頁面的url發現,所有的詳情頁的url域名都是一樣的,只有id不同
  • 可以通過獲取每一家企業的id結合着固定的域名拼接成詳情頁的url
  • 對url發起請求就可以獲取企業詳情數據

 

#爬取葯監局相關的數據http://125.35.6.84:81/xk/
url = 'http://125.35.6.84:81/xk/itownet/portalAction.do?method=getXkzsList'
data = {
    "on": "true",
    "pageSize": "15",
    "productName": "",
    "conditionType": "1",
    "applyname": "",
    "applysn": "",
}
page_text = requests.post(url=url,headers=headers,data=data).json()

for dic in page_text['list']:
    _id = dic['ID']
  #拿到ID后發現詳情頁面也是動態加載的,所以上面的步驟要重新來一遍,就可以拿到詳情頁面的數據了,注意請求方式為post detail_url
= 'http://125.35.6.84:81/xk/itownet/portalAction.do?method=getXkzsById' data = { 'id':_id } detail_data = requests.post(url=detail_url,headers=headers,data=data).json() print(detail_data)

 爬取圖片

  •  content返回的是二進制byte形式的響應數據
  •  兩種方法:1是用requests,2是用urllib
#如何爬取圖片
url = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1560235823355&di=4f6c6ad96539247edda08e7ad09f0dee&imgtype=0&src=http%3A%2F%2Fgss0.baidu.com%2F-fo3dSag_xI4khGko9WTAnF6hhy%2Fzhidao%2Fpic%2Fitem%2Fadaf2edda3cc7cd9e3670adb3901213fb80e916d.jpg'
#content返回的是二進制bytes形式的響應數據
img_data = requests.get(url=url,headers=headers).content

with open('./123.jpg','wb') as fp:
    fp.write(img_data)
from urllib import request
url = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1560235823355&di=4f6c6ad96539247edda08e7ad09f0dee&imgtype=0&src=http%3A%2F%2Fgss0.baidu.com%2F-fo3dSag_xI4khGko9WTAnF6hhy%2Fzhidao%2Fpic%2Fitem%2Fadaf2edda3cc7cd9e3670adb3901213fb80e916d.jpg'

request.urlretrieve(url=url,filename='./456.jpg')

 

 http協議:client和server進行數據交互的形式

https協議:http加密后

參考https://www.cnblogs.com/bobo-zhang/p/9645715.html

 ------------------------------------------------------------------------------------------------------------------------------------

回顧總結:

爬蟲的分類:

  通用爬蟲:爬取一整個頁面

  聚焦爬蟲:爬取頁面的某一部分

  增量式爬蟲:對新增內容的爬取

requests的使用

  作用:模擬瀏覽器發請求

  get/post:參數url params/data,headers

  反爬機制:

    robots:不需要做任何事情,直接可以爬取

    UA檢測:UA偽裝   User-Agent

  編碼流程:

    指定url  

    發起請求

    獲取響應數據

    持久化存儲

  get/post返回值:響應對象response

    text:字符串形式的響應數據

    json():返回的是標准的json數據,也就是字典

    content:二進制形式的響應數據

    encoding:響應數據的編碼

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM