爬蟲網路請求模組urllib

2021-10-18 16:51:33 字數 1358 閱讀 2315

url:統一資源定位符( uniform resource locator)

https: 協議

new.qq.com : 主機名(網域名稱) 省略了埠 443

omn/twf20200/twf2020032502924000.html 訪問資源的路徑

anchor : 錨點 前端用來做頁面定位或者導航

from urllib import request

url =

''request.urlretrieve(url,

'code2.png'

)

import urllib.request

url =

''headers =

# 1.建立請求的物件(構建user-agent)

req = urllib.request.request(url, headers=headers)

# 2.獲取響應物件(urlopen)

res = urllib.request.urlopen(req)

# 3.讀取響應物件的內容read().decode('utf-8')

html = res.read(

).decode(

'utf-8'

)print

(html)

print

(res.getcode())

# 獲取狀態碼

print

(res.geturl())

# url

import urllib.parse

import urllib.request

base_url =

's?'

key =

input

('請輸入您要搜尋的內容:'

)wd =

# urlencode() 傳遞乙個字典,進行中文處理

key = urllib.parse.urlencode(wd)

url = base_url + key

headers =

req = urllib.request.request(url,headers=headers)

res = urllib.request.urlopen(req)

html = res.read(

).decode(

'utf-8'

)# 儲存檔案

with

open

('搜尋.html'

,'w'

,encoding=

'utf-8'

)as f:

f.write(html)

Python爬蟲 網路請求 urllib

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

爬蟲 urllib的get請求

quote 方法 是將漢字轉換成unicode編碼 import urllib.request import urllib.parse url 請求物件的定製是為了解決反爬的第一種手段 headers 將 周杰倫 三個漢字變成unicode編碼的格式 需要依賴urllib.parse name ur...

python 網路爬蟲 urllib

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