使用urllib的網路爬蟲

2021-10-03 17:44:44 字數 976 閱讀 6220

#重要的三個內容

#**#請求頭

#請求方式(open方式)

from urllib import request

1、定義請求

#構造請求頭

header =

#構造formdata

formdata =

#對formdata進行url編碼處理

data = request.parse.urlencode(formdata)

.encode(encoding =

'utf-8'

)#使用構造好的data,header構造請求

res = resquest.resquest(url, data = data, headers = header)

2、定義opener

#用request中的處理器建構函式構造處理器

handle = resquest.proxyhandler('''傳入''')

或者handle = resquest.httphandler(

)#設定opener

opener = resquest.buile_opener(handle)

3、向目標傳送請求,獲取網頁資訊

<

1>

#將opener設定為全域性

request.install_opener(opener)

rep = request.urlopen(res)

.read(

).decode(

)<

2>

#直接用opener傳送請求

opener.

open

(res)

.read(

).decode(

)

4、利用正規表示式獲取想要的資訊

正規表示式的相關資訊

python 網路爬蟲 urllib

1.網域名稱與ip位址 網域名稱 dns伺服器 ip位址 你的電腦先把網域名稱傳給dns伺服器,通過dns伺服器找到網域名稱所對應的ip位址,在傳回你的電腦進行訪問。2.呼叫urllib進行爬取 讀取資料 import urllib f urllib.urlopen print f.read 讀取狀...

Python爬蟲 網路請求 urllib

簡單的請求from urllib.request import urlopen 發起網路請求 response urlopen assert response.code 200print 請求成功 儲存請求的網頁 file變數接受open 函式返回的物件的 enter 返回結果 with open ...

爬蟲網路請求模組urllib

url 統一資源定位符 uniform resource locator https 協議 new.qq.com 主機名 網域名稱 省略了埠 443 omn twf20200 twf2020032502924000.html 訪問資源的路徑 anchor 錨點 前端用來做頁面定位或者導航 from ...