爬蟲 urllib的get請求

2022-10-11 09:30:14 字數 1072 閱讀 5670

quote()方法:是將漢字轉換成unicode編碼

import

urllib.request

import

urllib.parse

url = '

'#請求物件的定製是為了解決反爬的第一種手段

headers =

#將「周杰倫」三個漢字變成unicode編碼的格式

#需要依賴urllib.parse

name = urllib.parse.quote('

周杰倫')#

拼接請求的新url

url = url+name

#請求物件的定製

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

#模擬瀏覽器向伺服器傳送請求

response =urllib.request.urlopen(request)

#獲取響應的內容

content = response.read().decode('

utf-8')

#列印資料

print(content)

import

urllib.request

import

urllib.parse

base_url = '

'data =

new_data =urllib.parse.urlencode(data)

#請求資源路徑

url = base_url +new_data

headers =

#請求物件的定製

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

#模擬瀏覽器向伺服器傳送請求

response =urllib.request.urlopen(request)

#獲取網頁原始碼的資料

content = response.read().decode('

utf-8')

#列印資料

print(content)

05 爬蟲 urllib2 GET請求

urllib2預設只支援http https的get和post方法!ipython2 中的測試結果 in 1 import urllib in 2 word 通過urllib.urlencode 方法,將字典鍵值對按url編碼轉換,從而能被web伺服器接受。in 3 urllib.urlencode...

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 ...