[Python](http://my.oschina.net/jhao104/blog/647308?fromerr=X3ziCEMd)
==============================================================
Category
--------
1.
- get
- post
2. IP
3. Cookies
4.
5.
6.
7. gzip
8 .
1.
**get**
```
import urllib2
url = "http://www.baidu.com"
response = urllib2.urlopen(url)
print response.read()
```
**post**
```
import urllib
import urllib2
url = "http://abcde.com"
form = {'name':'abc','password':'1234'}
form_data = urllib.urlencode(form)
request = urllib2.Request(url,form_data)
response = urllib2.urlopen(request)
print response.read()
```
2. IP
```
import urllib2
proxy = urllib2.ProxyHandler({'http': '127.0.0.1:8087'})
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)
response = urllib2.urlopen('http://www.baidu.com')
print response.read()
```
3. Cookies
```
import urllib2, cookielib
cookie_support= urllib2.HTTPCookieProcessor(cookielib.CookieJar())
opener = urllib2.build_opener(cookie_support)
urllib2.install_opener(opener)
content = urllib2.urlopen('http://XXXX').read()
```
CookieJar()HTTP cookieHTTPcookieHTTPcookiecookieCookieJarcookie
```
cookie = "PHPSESSID=91rurfqm2329bopnosfu4fvmu7; kmsign=55d2c12c9b1e3; KMUID=b6Ejc1XSwPq9o756AxnBAg="
request.add_header("Cookie", cookie)
```
4.
```
import urllib2
headers = {
'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'
}
request = urllib2.Request(
url = 'http://my.oschina.net/jhao104/blog?catalog=3463517',
headers = headers
)
print urllib2.urlopen(request).read()
```
5.
> lxmlhttp://my.oschina.net/jhao104/blog/639448
> BeautifulSouphttp://cuiqingcai.com/1319.html
BeautifulsouppythonHTMLlxmlCXpath
7. gzip
web 60% XML web XML
```
import urllib2, httplib
request = urllib2.Request('http://xxxx.com')
request.add_header('Accept-encoding', 'gzip') 1
opener = urllib2.build_opener()
f = opener.open(request)
```
:
```
import StringIO
import gzip
compresseddata = f.read()
compressedstream = StringIO.StringIO(compresseddata)
gzipper = gzip.GzipFile(fileobj=compressedstream)
print gzipper.read()
```
8.
```
from threading import Thread
from Queue import Queue
from time import sleep
# q
#NUM
#JOBS
q = Queue()
NUM = 2
JOBS = 10
#
def do_somthing_using(arguments):
print arguments
#
def working():
while True:
arguments = q.get()
do_somthing_using(arguments)
sleep(1)
q.task_done()
#fork NUM
for i in range(NUM):
t = Thread(target=working)
t.setDaemon(True)
t.start()
#JOBS
for i in range(JOBS):
q.put(i)
#JOBS
q.join()
```