1. 程式人生 > >python3.x module 'urllib' has no attribute 'urlopen' 或 ‘urlencode’問題解決方法

python3.x module 'urllib' has no attribute 'urlopen' 或 ‘urlencode’問題解決方法

問題

最近在使用Python的第三方模組 urllib 中的urlencode方法將字典編碼,用於提交資料給url等操作,發現urllib 下並沒有urlencode 和openurl,原來是因為在python3和python2下urllib模組中提供的urlencode 和openurl 位置不同

解決

最好的解決辦法就是找到 urllib 庫文件


python2

python2 中提供了urllib 和 urllib2 兩個模組

import urllib
urllib.urlencode(values)#其中values為所需要編碼的資料,並且只能為字典

python3

python3 中也有urllib和urllib3 兩個庫,其中urllib幾乎是python2 中urllib和urllib兩個模組的集合,我們經常也最使用urllib模組,而urllib3 則作為一個拓展模組使用。

urllib.parse.urlencode(query, doseq=False, safe='', encoding=None, errors=None, quote_via=quote_plus)

應用

現我們模擬登陸CSDN網站:

from urllib import request
from urllib import parse
from urllib.request import urlopen

values = {'username': '[email protected]', 'password': 'XXXX'}
data = parse.urlencode(values).encode('utf-8')  # 提交型別不能為str,需要為byte型別
url = 'https://passport.csdn.net/account/login?from=http://my.csdn.net/my/mycsdn'
request = request.Request(url, data)
response = urlopen(request)
print(response.read().decode())

注意:

將對映物件或可能包含str或bytes物件的兩元素元組的序列轉換為百分比編碼的ASCII文字字串。

如果生成的字串要作為用於使用urlopen()函式進行POST操作的資料,則應將其編碼為位元組,否則將導致TypeError。

urlopen

python2 

import urllib
urllib.urlopen(url,data = None,[timeout,] *,cafile = None,capath = None,cadefault = False,context = None)

Python3

在python3 中urlopen 在request模組下:

import urllib
urllib.request.urlopen(url,data = None,[timeout,] *,cafile = None,capath = None,cadefault = False,context = None)


相關推薦

python3.x module 'urllib' has no attribute 'urlopen' urlencode’問題解決方法

問題最近在使用Python的第三方模組 urllib 中的urlencode方法將字典編碼,用於提交資料給url等操作,發現urllib 下並沒有urlencode 和openurl,原來是因為在python3和python2下urllib模組中提供的urlencode 和o

Python3.X出現AttributeError: module 'urllib' has no attribute 'urlopen'錯誤

研究用Python寫爬蟲,下載一個網頁。報錯程式碼如下 import urllib def getHtml(url): page = urllib.urlopen(url) html = page.read() return html html

AttributeError: module 'urllib' has no attribute 'urlopen'

#!/usr/bin/python # -*- coding: UTF-8 -*- import urllib   def getHtml(url):       page = urllib.urlopen(url).read()       html=page.rea

python3:AttributeError: module 'urllib' has no attribute 'quote'

python2 與python3版本不相容的問題真是讓人詬病,現在又報錯了,我的錯誤資訊為: Traceback (most recent call last): File "/Users/eri

Python 3.x中使用urllib出現AttributeError: module 'urllib' has no attribute 'request'錯誤

剛剛開始學習爬蟲,開始寫部落格打算把錯誤記錄下來,已杜自己忘記,並給同樣的小白幫助python 3.x中urllib庫和urilib2庫合併成了urllib庫,python3.X中應該使用urllib.request,即替換掉(python中的)urllib2成urllib.

Python基礎學習-'module' object has no attribute 'urlopen'解決方法

參考“http://blog.sina.com.cn/s/blog_5cf74e410102uxsg.html” 用的是python 3.4 非常簡單的一小段程式碼 #!/usr/bin/python # -*- coding: UTF-8 -*- import ur

AttributeError: 'module' object has no attribute 'urlopen'

編譯環境:python 3.1.2 測試程式: # coding = utf-8 import urllib def getHtml(url):     page = urllib.

module 'urllib' has no attribute 'request'

import urllib # 網路上圖片的地址 img_src = 'http://img.my.csdn.net/uploads/201212/25/1356422284_1112.jpg' # 將遠端資料下載到本地,第二個引數就是要儲存到本地的檔名 urllib.r

關於 AttributeError: module 'urllib' has no attribute 'request' 的引包範圍問題

今天在使用python進行爬蟲的時候,遇到了一個小問題:AttributeError: module 'urllib' has no attribute 'request' AttributeError: module 'urllib' has no attribute 'p

創建django項目出現AttrbuteError module 'pip' has no attribute 'main'解決方法

python -- alt tee nal internal import ring 兩種 由於升級pip到10.0,創建django出現AttrbuteError module ‘pip‘ has no attribute ‘main‘錯誤,解決方法有兩種 一:找到/Ap

【pycharm】pycharm上安裝tensorflow,報錯:AttributeError: module 'pip' has no attribute 'main' 解決方法

pycharm上安裝tensorflow,報錯:AttributeError: module 'pip' has no attribute 'main' 解決方法     解決方法: 在pycharm的安裝目錄下,找到helpers/packaging_tool.py檔案,

pytorch實踐中module 'torch' has no attribute 'form_numpy'問題的解決

最近開始仔細玩了一下pytorch,發現裡面有個BUG之前都沒有發現。 在測試torch最基本的示例的情況下,居然碰到了個pytorch無法轉化numpy為Tensor的問題,呈現的問題如下: [em

Python指令碼報錯:AttributeError:'model' object has no attribute 'XXX'解決方法

錯誤舉例: 錯位原因定位: 檢視import庫的原始檔,發現原始檔存在且沒有錯誤,同時存在原始檔的.pyc檔案 解決方法: 1、命名py指令碼時,不要與python預留字,模組名等相同 例如:模組是ssl,請不要命名ssl.py檔案,因為程式碼執行時會對應生成.pyc檔案。

unittest中報錯:AttributeError: 'TestLogin' object has no attribute 'driver'解決方法

object login 技術分享 17. ive drive attr info unit 源代碼如下: 更改後: 執行成功。unittest中報錯:AttributeError: 'TestLogin' object has no attribut

Python3異常-AttributeError: module 'sys' has no attribute 'setdefaultencoding'

基於python3.6.1版本,在一個.py檔案中,加入這3行: import requests, re, sys reload(sys) sys.setdefaultencoding("utf-8"

error: ‘module’ object has no attribute ‘_rebuild_tensor_v2’

bject itl func hooks except attribute def ttr imp import torch._utils try: torch._utils._rebuild_tensor_v2 except AttributeError:

針對AttributeError: ‘module’ object has no attribute’xxx’的錯誤歸類

找不到 with 類型 error: 開頭 -a 發現 使用 def 目前遇見的有三種類型: 拼寫錯誤,模塊一定要拼寫錯誤,這個也是最容易犯的,發現找不到模塊的時候,最好先檢查一遍自己引入的模塊拼寫尤其是那些名字非常長的比如HTTPPasswordMgrWithDefau

module ‘markdown‘ has no attribute ‘version‘

anaconda har evel pycha 定位 oca arm 2.6 ttr 最近在寫一個CMDB的項目,遇到drf與django版本問題... 錯誤如下: AttributeError at / module ‘markdown‘ has no attribute

解決pycharm問題:module ‘pip‘ has no attribute ‘main‘

article 卸載 根據 sta 建議 pack blog htm tin 一:可能性解決方法一《解決pycharm問題:module ‘pip‘ has no attribute ‘main‘》http://www.cnblogs.com/Fordestiny/p/89

module 'queue' has no attribute 'Queue'解決

import queue q = queue.Queue() q.put('a') q.put('b') print(q.get()) 錯誤: AttributeError: module 'queue' has no attribute 'Queue' 解決方法:  把程