Python3 urllib抓取指定URL的內容

2021-07-04 12:38:12 字數 891 閱讀 3087

python爬蟲主要使用的是urllib模組,python2.x版本是urllib2,很多部落格裡面的示例都是使用urllib2的,因為我使用的是python3.3.2,所以在文件裡面沒有urllib2這個模組,import的時候會報錯,找不到該模組,應該是已經將他們整合在一起了。

下面是乙個簡單的**示例:

[python]view plain

copy

#encoding:utf-8

import

urllib.request  

defgetdata():  

url=""

data=urllib.request.urlopen(url).read()  

print

(data)  

getdata()  

結果:

中文轉碼,修改一下**:

[python]view plain

copy

#encoding:utf-8

import

urllib.request  

defgetdata():  

url=""

data=urllib.request.urlopen(url).read()  

z_data=data.decode('utf-8'

)  print

(z_data)  

getdata()  

結果如下:

python3 urllib爬蟲抓取記錄

import re import os from urllib import request 抓取整個頁面下來 data request.urlopen read decode 正則提取所有文章標題,ruler re.compile re.s match ruler.findall data 把抓取...

python3 urllib使用debug輸出

python2.7.5中使用debug輸出,可以採用如下方式 python3 中統一使用的是urllib模組庫,將python2中的urllib和urllib2進行了整合,試圖按上述方式編寫 如下 python3.4.2 window7 cmd 沒有語法錯誤提示,但是,也沒有任何除錯資訊出來。還有另...

Python3 urllib庫爬蟲 基礎

add header 新增報頭url req urllib.request.request url req.add header user agent mozilla 5.0 x11 ubuntu linux x86 64 rv 56.0 gecko 20100101 firefox 56.0 da...