當前位置:首頁 » 編程語言 » pythonurllib

pythonurllib

發布時間: 2022-08-03 22:10:03

python3.4沒有urllib2怎麼辦

python 3.x中urllib庫和urilib2庫合並成了urllib庫。

其中urllib2.urlopen()變成了urllib.request.urlopen()

urllib2.Request()變成了urllib.request.Request()

② python urllib2的用法

urllib2 默認會使用環境變數 http_proxy 來設置 HTTP Proxy。如果想在程序中明確控制 Proxy 而不受環境變數的影響,可以使用下面的方式:
import urllib2
enable_proxy = True
proxy_handler = urllib2.ProxyHandler({"http" : 'IP:8080'})
null_proxy_handler = urllib2.ProxyHandler({})
if enable_proxy:
opener = urllib2.build_opener(proxy_handler)
else:
opener = urllib2.build_opener(null_proxy_handler)
urllib2.install_opener(opener)
這里要注意的一個細節,使用 urllib2.install_opener() 會設置 urllib2 的全局 opener 。這樣後面的使用會很方便,但不能做更細粒度的控制,比如想在程序中使用兩個不同的 Proxy 設置等。比較好的做法是不使用 install_opener 去更改全局的設置,而只是直接調用 opener 的 open 方法代替全局的 urlopen 方法。

③ python3怎麼安裝 urllib

不需要安裝,自帶的
直接import urllib就可以。

④ python urllib怎麼用

python 3.x中urllib庫和urilib2庫合並成了urllib庫。。其中urllib2.urlopen()變成了urllib.request.urlopen() urllib2.Request()變成了urllib.request.Request() ……

⑤ python2.7 怎樣集成 urllib2

python最惡心的地方就在於它的版本和配置了,特別是安裝第三方包的時候經常會出現莫名其妙的錯誤,又不懂。

所以只能不斷的切來切去的。

今天學習python爬蟲,其中Python2.7使用了urllib和urllib2,python3的urllib結合了py2.7的兩部分。但是電腦不知為什麼又安裝不了py3的urllib,好煩。出現下面的錯誤。

python2.7和python3主要是模塊的位置變化地方較多。

其中python2.7的urllib和urllib2的區別一下:

  • urllib2可以接受一個Request類的實例來設置URL請求的headers,urllib僅可以接受URL。這意味著,你不可以通過urllib模塊偽裝你的User Agent字元串等(偽裝瀏覽器)。

  • urllib提供urlencode方法用來GET查詢字元串的產生,而urllib2沒有。這是為何urllib常和urllib2一起使用的原因。

  • urllib2模塊比較優勢的地方是urlliburllib2.urlopen可以接受Request對象作為參數,從而可以控制HTTP Request的header部。

  • 但是urllib.urlretrieve函數以及urllib.quote等一系列quote和unquote功能沒有被加入urllib2中,因此有時也需要urllib的輔助。

⑥ python3.4中urllib 有沒有urlencode函數

python3.x中urlencode在urllib.parse模塊中
使用方式urllib.parse.urlencode
urllib.parse.urlencode(query,
doseq=False, safe='', encoding=None,
errors=None, quote_via=quote_plus)

Convert a mapping object or a sequence of two-element tuples, which may
contain str
or bytes objects, to a 「percent-encoded」 string. If the
resultant string is to be used as a data for POST operation with urlopen() function, then it should be properly
encoded to bytes, otherwise it would result in a TypeError.

The resulting string is a series of key=value pairs separated by '&' characters, where
both key and value are quoted using the quote_via
function. By default, quote_plus() is used to quote the values, which
means spaces are quoted as a '+' character and 『/』 characters are encoded as %2F, which follows the
standard for GET requests (application/x-www-form-urlencoded). An alternate
function that can be passed as quote_via is quote(), which will encode spaces as %20 and not encode 『/』
characters. For maximum control of what is quoted, use quote and specify a value
for safe.

When a sequence of two-element tuples is used as the query argument,
the first element of each tuple is a key and the second is a value. The value
element in itself can be a sequence and in that case, if the optional parameter
doseq is evaluates to True, indivial key=value pairs separated
by '&' are
generated for each element of the value sequence for the key. The order of
parameters in the encoded string will match the order of parameter tuples in the
sequence.

The safe, encoding, and errors parameters are
passed down to quote_via (the encoding and errors
parameters are only passed when a query element is a str).

To reverse this encoding process, parse_qs() and parse_qsl() are provided in this mole to parse
query strings into Python data structures.

Refer to urllib examples to
find out how urlencode method can be used for generating query string for a URL
or data for POST.

Changed in version 3.2: Query parameter
supports bytes and string objects.

⑦ python的httplib,urllib和urllib2的區別及用

宗述
首先來看一下他們的區別
urllib和urllib2
urllib 和urllib2都是接受URL請求的相關模塊,但是urllib2可以接受一個Request類的實例來設置URL請求的headers,urllib僅可以接受URL。
這意味著,你不可以偽裝你的User Agent字元串等。
urllib提供urlencode方法用來GET查詢字元串的產生,而urllib2沒有。這是為何urllib常和urllib2一起使用的原因。
目前的大部分http請求都是通過urllib2來訪問

httplib
httplib實現了HTTP和HTTPS的客戶端協議,一般不直接使用,在更高層的封裝模塊中(urllib,urllib2)使用了它的http實現。

urllib簡單用法
urllib.urlopen(url[, data[, proxies]]) :
[python] view plain

google = urllib.urlopen('')
print 'http header:/n', google.info()
print 'http status:', google.getcode()
print 'url:', google.geturl()
for line in google: # 就像在操作本地文件
print line,
google.close()

詳細使用方法見
urllib學習

urllib2簡單用法
最簡單的形式
import urllib2
response=urllib2.urlopen(')
html=response.read()

實際步驟:
1、urllib2.Request()的功能是構造一個請求信息,返回的req就是一個構造好的請求
2、urllib2.urlopen()的功能是發送剛剛構造好的請求req,並返回一個文件類的對象response,包括了所有的返回信息。
3、通過response.read()可以讀取到response裡面的html,通過response.info()可以讀到一些額外的信息。
如下:

#!/usr/bin/env python
import urllib2
req = urllib2.Request("")
response = urllib2.urlopen(req)
html = response.read()
print html

有時你會碰到,程序也對,但是伺服器拒絕你的訪問。這是為什麼呢?問題出在請求中的頭信息(header)。 有的服務端有潔癖,不喜歡程序來觸摸它。這個時候你需要將你的程序偽裝成瀏覽器來發出請求。請求的方式就包含在header中。
常見的情形:

import urllib
import urllib2
url = '
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'# 將user_agent寫入頭信息
values = {'name' : 'who','password':'123456'}
headers = { 'User-Agent' : user_agent }
data = urllib.urlencode(values)
req = urllib2.Request(url, data, headers)
response = urllib2.urlopen(req)
the_page = response.read()

values是post數據
GET方法
例如網路:
,這樣我們需要將{『wd』:』xxx』}這個字典進行urlencode

#coding:utf-8
import urllib
import urllib2
url = ''
values = {'wd':'D_in'}
data = urllib.urlencode(values)
print data
url2 = url+'?'+data
response = urllib2.urlopen(url2)
the_page = response.read()
print the_page

POST方法

import urllib
import urllib2
url = ''
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' //將user_agent寫入頭信息
values = {'name' : 'who','password':'123456'} //post數據
headers = { 'User-Agent' : user_agent }
data = urllib.urlencode(values) //對post數據進行url編碼
req = urllib2.Request(url, data, headers)
response = urllib2.urlopen(req)
the_page = response.read()

urllib2帶cookie的使用

#coding:utf-8
import urllib2,urllib
import cookielib

url = r''

#創建一個cj的cookie的容器
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
#將要POST出去的數據進行編碼
data = urllib.urlencode({"email":email,"password":pass})
r = opener.open(url,data)
print cj

httplib簡單用法
簡單示例

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import httplib
import urllib

def sendhttp():
data = urllib.urlencode({'@number': 12524, '@type': 'issue', '@action': 'show'})
headers = {"Content-type": "application/x-www-form-urlencoded",
"Accept": "text/plain"}
conn = httplib.HTTPConnection('bugs.python.org')
conn.request('POST', '/', data, headers)
httpres = conn.getresponse()
print httpres.status
print httpres.reason
print httpres.read()

if __name__ == '__main__':
sendhttp()

具體用法見
httplib模塊
python 3.x中urllib庫和urilib2庫合並成了urllib庫。其中、
首先你導入模塊由
import urllib
import urllib2
變成了
import urllib.request

然後是urllib2中的方法使用變成了如下
urllib2.urlopen()變成了urllib.request.urlopen()
urllib2.Request()變成了urllib.request.Request()

urllib2.URLError 變成了urllib.error.URLError
而當你想使用urllib 帶數據的post請求時,
在python2中
urllib.urlencode(data)

而在python3中就變成了
urllib.parse.urlencode(data)

腳本使用舉例:
python 2中

import urllib
import urllib2
import json
from config import settings
def url_request(self, action, url, **extra_data): abs_url = "http://%s:%s/%s" % (settings.configs['Server'],
settings.configs["ServerPort"],
url)
if action in ('get', 'GET'):
print(abs_url, extra_data)
try:
req = urllib2.Request(abs_url)
req_data = urllib2.urlopen(req, timeout=settings.configs['RequestTimeout'])
callback = req_data.read()
# print "-->server response:",callback
return callback

except urllib2.URLError as e:
exit("\033[31;1m%s\033[0m" % e)
elif action in ('post', 'POST'):
# print(abs_url,extra_data['params'])
try:
data_encode = urllib.urlencode(extra_data['params'])
req = urllib2.Request(url=abs_url, data=data_encode)
res_data = urllib2.urlopen(req, timeout=settings.configs['RequestTimeout'])
callback = res_data.read()
callback = json.loads(callback)
print("\033[31;1m[%s]:[%s]\033[0m response:\n%s" % (action, abs_url, callback))
return callback
except Exception as e:
print('---exec', e)
exit("\033[31;1m%s\033[0m" % e)

python3.x中

import urllib.request
import json
from config import settings

def url_request(self, action, url, **extra_data):
abs_url = 'http://%s:%s/%s/' % (settings.configs['ServerIp'], settings.configs['ServerPort'], url)
if action in ('get', 'Get'): # get請求
print(action, extra_data)try:
req = urllib.request.Request(abs_url)
req_data = urllib.request.urlopen(req, timeout=settings.configs['RequestTimeout'])
callback = req_data.read()
return callback
except urllib.error.URLError as e:
exit("\033[31;1m%s\033[0m" % e)
elif action in ('post', 'POST'): # post數據到伺服器端
try:
data_encode = urllib.parse.urlencode(extra_data['params'])
req = urllib.request.Request(url=abs_url, data=data_encode)
req_data = urllib.request.urlopen(req, timeout=settings.configs['RequestTimeout'])
callback = req_data.read()
callback = json.loads(callback.decode())
return callback
except urllib.request.URLError as e:
print('---exec', e)
exit("\033[31;1m%s\033[0m" % e)

settings配置如下:

configs = {
'HostID': 2,
"Server": "localhost",
"ServerPort": 8000,
"urls": {

'get_configs': ['api/client/config', 'get'], #acquire all the services will be monitored
'service_report': ['api/client/service/report/', 'post'],

},
'RequestTimeout': 30,
'ConfigUpdateInterval': 300, # 5 mins as default

}

⑧ urllib.parse在python2.7中怎麼用

最新版的python3.3.0已經發布了。相較於python3.0,3.2的改動並不大。但網上的大量的教程等大都以2.x版本為基礎。這為想要從python3.0學起的菜鳥帶來了不少的困難。 作為一隻菜鳥,最近想學習一下python中urllib模塊的使用方法。從網上找的最簡單的實例:把google 首頁的html抓取下來並顯示在控制台上 代碼:

[python]view plain

  • importurllib

  • printurllib.urlopen('http://www.google.com').read()


  • 首先,使用過python3.0的朋友都知道,print已經變成含樹了,需要括弧。但這不是主要問題。問題是控制台顯示錯誤,說urllib模塊中沒有urlopen方法。 奇怪了,網上的教程能錯了?又嘗試help(urllib),發現什麼方法都沒有,只提供了package contents,裡面有5個名字。

  • [python]view plain

  • importurllib

  • help(urllib)


  • 3.0版本中已經將urllib2、urlparse、和robotparser並入了urllib中,並且修改urllib模塊,其中包含5個子模塊,即是help()中看到的那五個名字。

    為了今後使用方便,在此將每個包中包含的方法列舉如下:

    urllib.error:ContentTooShortError; HTTPError; URLError

    urllib.parse:parseqs; parseqsl; quote; quotefrombytes; quote_plus; unquote unquoteplus; unquoteto_bytes; urldefrag; urlencode; urljoin;urlparse; urlsplit; urlunparse; urlunsplit

    urllib.request:AbstractBasicAuthHandler; AbstractDigestAuthHandler; BaseHandler; CatheFTPHandler; FTPHandler; FancyURLopener; FileHandler; HTTPBasicAuthHandler; HTTPCookieProcessor; HTTPDefaultErrorHandler; HTTPDigestAuthHandler; HTTPErrorProcessorl; HTTPHandler; HTTPPasswordMgr; ; HTTPRedirectHandler; HTTPSHandler;OpenerDirector;ProxyBasicAuthHandler ProxyDigestAuthHandler; ProxyHandler; Request; URLopener; UnknowHandler; buildopener; getproxies; installopener; pathname2url; url2pathname; urlcleanup;urlopen; urlretrieve;

    urllib.response:addbase; addclosehook; addinfo; addinfourl;

    urllib.robotparser:RobotFileParser

  • ---------------------------------------------------------------------------------------------------------
  • 在2.X版本下,打開HTML文檔的實例:

    [python]view plain

  • importurllib

  • webURL="http://www.python.org"

  • localURL="index.html"

  • #通過URL打開遠程頁面

  • u=urllib.urlopen(webURL)

  • buffer=u.read()

  • printu.info()

  • print"從%s讀取了%d位元組數據."%(u.geturl(),len(buffer))

  • #通過URL打開本地頁面

  • u=urllib.urlopen(localURL)

  • buffer=u.read()

  • printu.info()

  • print"從%s讀取了%d位元組數據."%(u.geturl(),len(buffer))


  • 運行結果如下:
  • [html]view plain

  • Date:Fri,26Jun200910:22:11GMT

  • Server:Apache/2.2.9(Debian)DAV/2SVN/1.5.1mod_ssl/2.2.9OpenSSL/0.9.8gmod_wsgi/2.3Python/2.5.2

  • Last-Modified:Thu,25Jun200909:44:54GMT

  • ETag:"105800d-46e7-46d29136f7180"

  • Accept-Ranges:bytes

  • Content-Length:18151

  • Connection:close

  • Content-Type:text/html

  • 從http://www.python.org讀取了18151位元組數據.

  • Content-Type:text/html

  • Content-Length:865

  • Last-modified:Fri,26Jun200910:16:10GMT

  • 從index.html讀取了865位元組數據.

  • 若要通過urllib模塊中的urlopen(url [,data])函數打開一個HTML文檔,必須提供該文檔的URL地址,包括文件名。函數urlopen不僅可以打開位於遠程web伺服器上的文件,而 且可以打開一個本地文件,並返回一個類似文件的對象,我們可以通過該對象從HTML文檔中讀出數據。

    一旦打開了HTML文檔,我們就可以像使用常規文件一樣使用read([nbytes])、readline()和readlines()函數來對文件進行讀操作。若要讀取整個HTML文檔的內容的話,您可以使用read()函數,該函數將文件內容作為字元串返回。

    打開一個地址之後,您可以使用geturl()函數取得被獲取網頁的真正的URL。這是很有用的,因為urlopen(或使用的opener對象)也許會伴隨一個重定向。獲取的網頁URL也許和要求的網頁URL不一樣。

    另一個常用的函數是位於從urlopen返回的類文件對象中的info()函數,這個函數可以返回URL位置有關的元數據,比如內容長度、內容類型,等等。下面通過一個較為詳細的例子來對這些函數進行說明。

    --------------------------------------------------------------------------------------------------------------------------

    在2.X版本下,urlparse使用實例:

    [python]view plain

  • importurlparse

  • URLscheme="http"

  • URLlocation="www.python.org"

  • URLpath="lib/mole-urlparse.html"

  • modList=("urllib","urllib2",

  • "httplib","cgilib")

  • #將地址解析成組件

  • print"用Google搜索python時地址欄中URL的解析結果"

  • parsedTuple=urlparse.urlparse(

  • "http://www.google.com/search?

  • hl=en&q=python&btnG=Google+Search")

  • printparsedTuple

  • #將組件反解析成URL

  • print"反解析python文檔頁面的URL"

  • unparsedURL=urlparse.urlunparse(

  • (URLscheme,URLlocation,URLpath,'','',''))

  • print" "+unparsedURL

  • #將路徑和新文件組成一個新的URL

  • print"利用拼接方式添加更多python文檔頁面的URL"

  • formodinmodList:

  • newURL=urlparse.urljoin(unparsedURL,

  • "mole-%s.html"%(mod))

  • print" "+newURL

  • #通過為路徑添加一個子路徑來組成一個新的URL

  • print"通過拼接子路徑來生成Python文檔頁面的URL"

  • newURL=urlparse.urljoin(unparsedURL,

  • "mole-urllib2/request-objects.html")

  • print" "+newURL


  • 運行結果如下:
  • [python]view plain

  • 用Google搜索python時地址欄中URL的解析結果

  • ('http','www.google.com','/search','',

  • 'hl=en&q=python&btnG=Google+Search','')

  • 反解析python文檔頁面的URL

  • http://www.python.org/lib/mole-urlparse.html

  • 利用拼接方式添加更多python文檔頁面的URL

  • http://www.python.org/lib/mole-urllib.html

  • http://www.python.org/lib/mole-urllib2.html

  • http://www.python.org/lib/mole-httplib.html

  • http://www.python.org/lib/mole-cgilib.html

  • 通過拼接子路徑來生成Python文檔頁面的URL

⑨ 如何在Python中使用urllib2

urllib2 默認會使用環境變數 http_proxy 來設置 HTTP Proxy。如果想在程序中明確控制 Proxy 而不受環境變數的影響,可以使用下面的方式:
import urllib2
enable_proxy = True
proxy_handler = urllib2.ProxyHandler({"http" : 'IP:8080'})
null_proxy_handler = urllib2.ProxyHandler({})
if enable_proxy:
opener = urllib2.build_opener(proxy_handler)
else:
opener = urllib2.build_opener(null_proxy_handler)
urllib2.install_opener(opener)
這里要注意的一個細節,使用 urllib2.install_opener() 會設置 urllib2 的全局 opener 。這樣後面的使用會很方便,但不能做更細粒度的控制,比如想在程序中使用兩個不同的 Proxy 設置等。比較好的做法是不使用 install_opener 去更改全局的設置,而只是直接調用 opener 的 open 方法代替全局的 urlopen 方法。

⑩ python urllib2模塊 在哪裡下載

urllib2是python自帶的模塊,不需要下載。

urllib2在python3.x中被改為urllib.request

熱點內容
國服沒有腳本嗎 發布:2025-03-13 02:52:57 瀏覽:702
機器人解壓 發布:2025-03-13 02:52:18 瀏覽:955
怎麼在伺服器上部署網站 發布:2025-03-13 02:52:15 瀏覽:207
android彈出鍵盤布局 發布:2025-03-13 02:46:22 瀏覽:379
單耳安卓藍牙耳機怎麼使用教程 發布:2025-03-13 02:36:22 瀏覽:518
配置apache以域名訪問 發布:2025-03-13 02:22:34 瀏覽:559
android視頻錄制播放 發布:2025-03-13 02:10:32 瀏覽:601
php從資料庫中讀取數據 發布:2025-03-13 02:10:27 瀏覽:942
熱血傳奇任務腳本 發布:2025-03-13 02:06:16 瀏覽:303
t4伺服器什麼牌子好 發布:2025-03-13 02:02:39 瀏覽:633