一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語(yǔ)言編程技術(shù)及教程分享平臺(tái)!
分類(lèi)導(dǎo)航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務(wù)器之家 - 腳本之家 - Python - Python urllib、urllib2、httplib抓取網(wǎng)頁(yè)代碼實(shí)例

Python urllib、urllib2、httplib抓取網(wǎng)頁(yè)代碼實(shí)例

2020-06-24 10:00Python教程網(wǎng) Python

這篇文章主要介紹了Python urllib、urllib2、httplib抓取網(wǎng)頁(yè)代碼實(shí)例,本文直接給出demo代碼,代碼中包含詳細(xì)注釋,需要的朋友可以參考下

使用urllib2,太強(qiáng)大了
試了下用代理登陸拉取cookie,跳轉(zhuǎn)抓圖片......
文檔:http://docs.python.org/library/urllib2.html

直接上demo代碼了
包括:直接拉取,使用Reuqest(post/get),使用代理,cookie,跳轉(zhuǎn)處理

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/python
# -*- coding:utf-8 -*-
# urllib2_test.py
# author: wklken
# 2012-03-17 [email protected]
 
 
import urllib,urllib2,cookielib,socket
 
url = "http://www.testurl....." #change yourself
#最簡(jiǎn)單方式
def use_urllib2():
 try:
  f = urllib2.urlopen(url, timeout=5).read()
 except urllib2.URLError, e:
  print e.reason
 print len(f)
 
#使用Request
def get_request():
 #可以設(shè)置超時(shí)
 socket.setdefaulttimeout(5)
 #可以加入?yún)?shù) [無(wú)參數(shù),使用get,以下這種方式,使用post]
 params = {"wd":"a","b":"2"}
 #可以加入請(qǐng)求頭信息,以便識(shí)別
 i_headers = {"User-Agent": "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1) Gecko/20090624 Firefox/3.5",
       "Accept": "text/plain"}
 #use post,have some params post to server,if not support ,will throw exception
 #req = urllib2.Request(url, data=urllib.urlencode(params), headers=i_headers)
 req = urllib2.Request(url, headers=i_headers)
 
 #創(chuàng)建request后,還可以進(jìn)行其他添加,若是key重復(fù),后者生效
 #request.add_header('Accept','application/json')
 #可以指定提交方式
 #request.get_method = lambda: 'PUT'
 try:
  page = urllib2.urlopen(req)
  print len(page.read())
  #like get
  #url_params = urllib.urlencode({"a":"1", "b":"2"})
  #final_url = url + "?" + url_params
  #print final_url
  #data = urllib2.urlopen(final_url).read()
  #print "Method:get ", len(data)
 except urllib2.HTTPError, e:
  print "Error Code:", e.code
 except urllib2.URLError, e:
  print "Error Reason:", e.reason
 
def use_proxy():
 enable_proxy = False
 proxy_handler = urllib2.ProxyHandler({"http":"http://proxyurlXXXX.com:8080"})
 null_proxy_handler = urllib2.ProxyHandler({})
 if enable_proxy:
  opener = urllib2.build_opener(proxy_handler, urllib2.HTTPHandler)
 else:
  opener = urllib2.build_opener(null_proxy_handler, urllib2.HTTPHandler)
 #此句設(shè)置urllib2的全局opener
 urllib2.install_opener(opener)
 content = urllib2.urlopen(url).read()
 print "proxy len:",len(content)
 
class NoExceptionCookieProcesser(urllib2.HTTPCookieProcessor):
 def http_error_403(self, req, fp, code, msg, hdrs):
  return fp
 def http_error_400(self, req, fp, code, msg, hdrs):
  return fp
 def http_error_500(self, req, fp, code, msg, hdrs):
  return fp
 
def hand_cookie():
 cookie = cookielib.CookieJar()
 #cookie_handler = urllib2.HTTPCookieProcessor(cookie)
 #after add error exception handler
 cookie_handler = NoExceptionCookieProcesser(cookie)
 opener = urllib2.build_opener(cookie_handler, urllib2.HTTPHandler)
 url_login = "https://www.yourwebsite/?login"
 params = {"username":"user","password":"111111"}
 opener.open(url_login, urllib.urlencode(params))
 for item in cookie:
  print item.name,item.value
 #urllib2.install_opener(opener)
 #content = urllib2.urlopen(url).read()
 #print len(content)
#得到重定向 N 次以后最后頁(yè)面URL
def get_request_direct():
 import httplib
 httplib.HTTPConnection.debuglevel = 1
 request = urllib2.Request("http://www.google.com")
 request.add_header("Accept", "text/html,*/*")
 request.add_header("Connection", "Keep-Alive")
 opener = urllib2.build_opener()
 f = opener.open(request)
 print f.url
 print f.headers.dict
 print len(f.read())
 
if __name__ == "__main__":
 use_urllib2()
 get_request()
 get_request_direct()
 use_proxy()
 hand_cookie()

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲香蕉视频 | 亚洲国产在线午夜视频无 | 欧美日韩精品在线视频 | 日本午夜vr影院新入口 | 熟睡中的麻麻大白屁股小说 | 久久精品久久久 | 欧美久久热| 国产精品毛片高清在线完整版 | 日本午夜vr影院新入口 | 4虎影院永久地址www | 国产一卡2卡3卡4卡公司科普 | 无人区免费一二三四乱码 | 1024在线视频精品免费 | 三级黄色片在线观看 | 耽美调教高h | 久久日本片精品AAAAA国产 | 日本高清动作片www欧美 | 麻豆夏晴子 | 国产精品露脸国语对白手机视频 | 国产福利在线观看第二区 | 亚洲精品国产乱码AV在线观看 | 免费一级欧美片在线观免看 | 成年女人毛片免费观看中文w | 欧美日韩综合网在线观看 | 国产精品猎奇系列在线观看 | 欧美精品亚洲精品日韩1818 | 日本xxxxxxxxx59| 99热精品在线播放 | 外国老少性配 | 国产精品香蕉一区二区三区 | 久久中文字幕亚洲精品最新 | s0e一923春菜花在线播放 | 久久强奷乱码老熟女 | 羞羞私人影院可以直接免费观影吗 | 日韩去日本高清在线 | 亚洲一区二区成人 | 亚洲国内精品 | videos欧美肥婆 | 69av免费视频 | 国产精品一久久香蕉产线看 | 女同学用白丝脚玩我的故事 |