Python's dictionary structure data in #{}, incoming UserName and Password#urlencode () The function encodes the dictionary structure into a specific data class req=urllib2. Request ( url = myurl, data = Self.postdata ) #给Request传入URL以及编码好的data数据myResponse = Urllib2.urlopen (req) MyPage = Myresponse.read ()C. Processing cookiesThere was less to consider a thing before. is to visit other pages of the site when you log in. A login cookie is required.There are no speci
There are many useful tool classes in the Python standard library, but it is not clear how to use the detail description on the standard library document, such as URLLIB2, which is the HTTP client library. Here is a summary of some of the URLLIB2 library usage details.
1 Proxy Settings
2 Timeout Settings
3 Adding a specific Header to the HTTP Request
4 Redirect
5 Cookies
6 PUT and DELETE methods using HTTP
7 Getting the return code for HTTP
8 Debug Log
1
Open (file, mode= ' R ', Buffering=-1, Encoding=none, Errors=none, Newline=none, Closefd=true, Opener=none):In the use of this function, in addition to the file parameter must be filled out, other parameters can be selected. The default values are used in this code for other parameters.When using open (), if the file does not exist, the ioerror will be returned.Parameter description:File: Document name;Mode: The way the file is opened, the function pr
is empty. The
httppasswordmgr is used to manage passwords, and it maintains a table of user name passwords.
Httpbasicauthhandler is used to manage authentication, and it can be used to resolve authentication issues If a link is open and requires authentication. More Handler can refer to Https://docs.python.org/3/library/urllib.request.html#basehandler-objects Urlopen is required in normal request, and handler is required to use Opener,
user logs on, the server will create a session for the user. The session corresponds to the user's profile. The file represents the user.What about a visit request that belongs to that user? When logged in, the server requires the browser to store a cookie value for the session ID. Each visit is brought with the cookie. The server will know which user the request came from by a match between the session ID in the cookie and the session ID in the server.OpenerWhen we call Urllib2.urlopen (URL),
Python encoding conversion and Chinese processingUnicode in Python is a confusing and difficult problem to understand. Utf-8 is an implementation of Unicode, Unicode, GBK, and gb2312 are coded character sets.Decode is to parse the normal string according to the encoding format in the parameter, and then generate the corresponding Unicode objectThe Chinese encoding problem encountered when writing Python:? /test sudo vim test.py#!/usr/bin/python#-*-Coding:utf-8-*-Def weather ():Import timeImport
RESP
# deflate Support
Import zlib
def deflate (data): # zlib only provides the zlib compress format, not the deflate format;
Try: # so on top of the all there ' s This workaround:
Return zlib. Decompress (data,-zlib). Max_wbits)
Except Zlib. Error:
Return zlib. Decompress (data)
Class Fetcher:
'''
HTML fetcher
Basic usage
-----------
From Fetcher Import Fetcher
f = fetcher ()
F.get (URL)
Post
----
req = Urllib2. Request (...)
F.post (req)
Multi-thread
------------
f = fetcher (thread
POST!!Set _XSRF, ID, password Three magic weapon, we can launch post. Once the post is launched, we log on to the server and the server sends US Cookies. It would be a hassle to deal with cookies, but the Python Http.cookiejar library gives us a convenient solution, so long as a httpcookieprocessor is put in when the opener is created, the cookie thing is not It's in our possession. The following code shows this point.12345678910111213Import Http.coo
It's just an article. It's not guaranteed to be usable if you keep an archive.
Use existing cookies to access the website
Import cookielib, urllib2
Ckjar = cookielib. cookillacookiejar (OS. Path. Join ('C: \ Documents and Settings \ Tom \ Application Data \ Mozilla \ Firefox \ profiles \ h5m61j1i. default ','Cookies.txt '))
Req = urllib2.request (URL, postdata, header)
Req. add_header ('user-agent ',\'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1 )')
Methods and attributes of js-window objects
Hxpd published on 21:58:18 proficient in the use of open, close, alert, confirm, prompt, setTimeout, cleartimeout, setinterval, clearinterval, moveBy, resizeBy, and scrollby methods of window objects
Measure the test taker's knowledge about how to use the moveTo, resizeTo, scrollto, and print methods of window objects.
Familiar with the use of window objects in the status, location, name, self, and opener
Do an analysis of the following codeImport= Urllib.urlencode ({'wd'python' = Urllib.urlopen ("http://www.baidu.com/s?%s" % params)Print F.read ()This is a simple code to read the URL contentThe key here is Urlopen, and by looking at it, you can see the Urlopen code as followsdefUrlopen (URL, Data=none, proxies=None):"""Create a File-like object for the specified URL to read from.""" fromWarningsImportwarnpy3k warnpy3k ("Urllib.urlopen () have been removed in Python 3.0 in" "favo
code is as follows:1 #Coding=utf-82 ImportUrllib23 4 #Create a Request object5Request= Urllib2. Request ("The URL that you want to crawl")6 7 #Add Data a=18Request.add_data ('a','1')9 Ten #add headers for HTTP OneRequest.add_header ('user-agent','mozilla/5.0') A - #send request Get results -Response=Urllib2.urlopen (Request) the - PrintResponse.getcode () - - PrintResponse.read ()method two code examplesMethod Three, add a special situation of the processorSome Web pages need to be logged in
= {' * * ': ' * * * ', ' * * ': ' * * * ' , ' * * ': ' * * * '} #使用urllib. The UrlEncode function processes the values dictionary in the final form: **=*****=***data = Urllib.urlencode (values) # If the data order is required, it is recommended that you splice datareq = urllib2. Request (URL, data) response = Urllib2.urlopen (req) the_page = Response.read ()Like the code above, the Post method is written to the data parameter, and the post is simulated successfully.Second pi
need to analyze the page structure, build the form data post, encode with UrlEncode () , return the string, and then specify the ' utf-8 ' encoding format, because postdata can only be bytes or file object. Finally, pass the postdata through the request () object and send the requests using Urlopen () .Download Remote Data to localimport urllib.requesturl = "https://www.python.org/static/img/python-logo.png" Urllib.request.urlretrieve (URL, "python-logo.png")You can use Urlretrieve () to downlo
Settings for Proxy
URLLIB2 uses environment variable HTTP_PROXY to set HTTP proxy by default. If you want to explicitly control the Proxy in your program without being affected by the environment variables, you can use the following methods
Copy Code code as follows:
Import Urllib2
Enable_proxy = TrueProxy_handler = Urllib2. Proxyhandler ({"http": ' http://some-proxy.com:8080 '})Null_proxy_handler = Urllib2. Proxyhandler ({})
If Enable_proxy:Opener = Urllib2.build_open
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.