Original post address: http://hi.baidu.com/yss1983/item/933fbe45a09c43e01381da06
Problem Description:Not set the timeout parameters, the result is bad in the network environment, often read () method has no response to the problem, the program card died in read () method, engaged in the majority of days, only to find the problem, to Urlopen plus timeout on OK, Set the timeout after timeout read timeout will throw Socket.timeout exception, want to stab
No set timeout parameters, the result in the network environment is bad, often read () method without any response to the problem, the program card died in read () method, engaged in the majority of days, only to find problems, to Urlopen plus timeout on OK, set the timeout After the timeout after the read timeout will throw Socket.timeout exception, want to stabilize the program, but also need to Urlopen p
When sending a request to the site, there has been a 503 error, the code is as follows: HTTP Error 503: Service UnavailableQuery network reasons, mainly the other side of the automatic query is limited, you can modify the header to deceive him, but this method is not always valid, you can refer to the link:Http://stackoverflow.com/questions/25936072/python-urllib2-httperror-http-error-503-service-unavailable-on-valid-websiteBut I did a reset of the
Traceback (most recent): File "F:\adt-bundle-windows-x86_64-20140702\eclipse\workspace1\XueChe\src\xueche2 \0\xueche.py ", line A, in Run Python program unexpectedly error, then ... Actually is the Internet Explorer uses the agent, I go ah, this which with where, I use Fillder will automatically add 127.0.0.1 as IE agent, sweat, remove canPython run Error: urllib2. Urlerror:
# # Python implements the three-way HTTP request: Urllib2/urllib, Httplib/urllib, and requestsUrllib2/urllib implementation
Urllib2 and Urllib are python two built-in modules, to implement the HTTP function, the implementation is mainly URLLIB2, urllib as a supplement
1 first implementation of a complete request and response model
Deep understanding of Urllib, URLLIB2 and requests
650) this.width=650; "class=" Project-logo "src=" http://codefrom.oss-cn-hangzhou.aliyuncs.com/www/2015/06-03/ 00380d0fbed52c2b5d697152ed3922d6 "/> python
Python is an object-oriented, interpreted computer programming language, invented by Guido van Rossum at the end of 1989, the first public release was released in 1991, and the Python source code also follows the GPL (GNU general public
URLLIB2 is a component of Python's acquisition of URLs (Uniform Resource Locators). He provides a very simple interface in the form of a urlopen function, which is capable of acquiring URLs using different protocols, and he also provides a more complex interface to handle general situations such as basic authentication, cookies, proxies, and others. They are provided through handlers and openers objects.
First, the most basic application
Import urllib2
url = R ' http://www.baidu.com '
html = urllib2.urlopen (URL). Read ()
print HTML
Client and server side through request and response to communicate, the client first sends request to the service side, then receives the service side return response
The URLLIB2
You may be familiar with url-based HTTP requests and other operations using the urllib2 module. here, we will take a closer look at urllib2's HTTP exception handling functions, let's take a look at the usage summary of the urllib2 module in Python network programming:
I. basic applications
import urllib2url = r'http://www.baidu.com'html =
instead of the global Urlopen method.2.Timeout settingsIn older Python (before Python2.6), the Urllib2 API did not expose the timeout setting, and to set the timeout value, only the global timeout value of the Socket could be changed. Import URLLIB2 Import Socket Socket.setdefaulttimeout (10) # 10 seconds after timeout Urlli
Python write crawlers use the urllib2 method, pythonurllib2
Use urllib2 for python write Crawlers
The Usage Details of urllib2 are sorted out.
1. Proxy Settings
By default, urllib2 uses the environment variable http_proxy to set HTTP Proxy.
If you want to explicitly control the Proxy in the program without bein
instead of the global Urlopen Method.2.Timeout settingsIn the old version of Python (before Python2.6). The Urllib2 API does not expose the Timeout setting. To set the timeout value, you can only change the global timeout value of the Socket.Import urllib2 Import socket socket.setdefaulttimeout (10) # 10 seconds after timeout
Urlopen method.
2.Timeout settingsIn older Python (before Python2.6), the Urllib2 API did not expose the timeout setting, and to set the timeout value, only the global timeout value of the Socket could be changed.
Import urllib2 Import socket Socket.setdefaulttimeout (10) # 10 seconds after timeout urllib2.socket.s
simply calling the opener open method instead of the global Urlopen method.
2.Timeout settings
In the old Python (Python2.6), the Urllib2 API did not expose Timeout settings, and to set the Timeout value, only the Socket's global Timeout value could be changed.
Copy Code code as follows:
Import Urllib2
Import socket
Socket.setdefaulttimeou
Basic usage of the python urllib2 package1. urllib2.urlopen (request)
Url = "http://www.baidu.com" # url can also be the path of other protocols, such as ftpvalues = {'name': 'Michael Foord ', 'location': 'northampt', language ': 'python'} data = urllib. urlencode (values) user_agent = 'mozilla/4.0 (compatible; MSIE 5.5; Windows NT) 'headers = {'user-agent': user
Summary of the usage of the urllib2 module in Python network programming, pythonurllib2
I. Basic Applications
import urllib2url = r'http://www.baidu.com'html = urllib2.urlopen(url).read()print html
The client communicates with the server through request and response. The client first sends a request to the server and then receives the response returned by the ser
, instead of simply calling the opener open method instead of the global Urlopen method.2.Timeout settingsIn the old Python (Python2.6), the Urllib2 API did not expose Timeout settings, and to set the Timeout value, only the Socket's global Timeout value could be changed.[python] view plain copy import URLLIB2 import socket Socket.setdefaulttimeout (10) # timeout
Document directory
1. Capture simple web pages
2. Download an object
3. basic use of urllib
4. basic use of urllib2
I recently learned Python again. Unfortunately, I didn't use it for work. I could only use my spare time to play it out.1. Capture simple web pages
# coding=utf-8import urllib2response = urllib2.urlopen('http://www.pythonclub.org/python-net
settings, but instead uses the current data to replace the previousRequest.add_header (Key,val) key is the header name, Val is the header value, two parameters are stringRequest.add_unredirected_header (Key,val) Ibid, but not added to redirect requestRequest.set_proxy (Host,type) prepares the request to the server. Replace the original host with host and replace the original request type with type3, openerThe basic Urlopen () function does not suppor
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.