爬蟲請求模組

2021-10-05 14:43:16 字數 2329 閱讀 8765

2. urllib.parse

3. 請求方式

4. request模組

5. requests原始碼分析

位元組流 = response.read()

字串 = response.read().decode(「utf-8」)

urllib.request.request"**",headers=「字典」

import urllib.request

url =

''# response 是響應物件

response = urllib.request.urlopen(url)

print

(response.getcode(

),response.geturl(

))

使用流程:

import urllib.request

url =

''headers =

# 建立請求物件

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

# 獲取響應物件

response =urllib.request.urlopen(req)

# 讀取響應物件內容

("請輸入你要翻譯的內容:"

)data =

response = requests.post(url,data=data,headers=headers)

# 傳送post請求

response.encoding=

'utf-8'

html = response.text # print(type(html)) /

# print(html) /]]} 需轉換為字典型別方便提取結果

# json.loads() 可將 str 型別的 html ——> dict 型別

result = json.loads(html)

# print(type(result)) #

print

(result[

'translateresult'][

0][0

]['tgt'

])

# ****

# 西刺免費**ip:

# 快**:

# **雲: -- 比較麻煩

import requests

# 設定**

proxy =

#從以上****中尋找可用**ip

url =

''res = requests.get(url,proxies = proxy)

#**測試

)# 指定嘗試連線

print

(response.text)

# 成功

爬蟲請求模組

1 版本 python2 urllib urllib2 python3 把urllib 和 urllib2合併,urllib.request 2 常用方法 2.1.1位元組流 response.read 字串 response.read decode utf 8 31 encode 字串 bytes...

Python爬蟲02 請求模組

七 json資料 response.text 返回unicode格式的資料 str response.content 返回位元組流資料 二進位制 response.content.decode utf 8 手動進行解碼 response.url 返回url response.encode 編碼 im...

爬蟲網路請求模組urllib

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