华南俳烁实业有限公司

python

當前位置:中華考試網(wǎng) >> python >> python教程 >> 文章內(nèi)容

舉例告訴你python爬蟲庫urllib2怎么用

來源:中華考試網(wǎng)  [2020年9月30日]  【

   所謂網(wǎng)頁抓取,就是把URL地址中指定的網(wǎng)絡資源從網(wǎng)絡流中讀取出來,保存到本地。 在Python中有很多庫可以用來抓取網(wǎng)頁,先學習urllib2。

  urllib2模塊直接導入就可以用,在python3中urllib2被改為urllib.request

  使用urllib2,試了下用代理登陸拉取cookie,跳轉(zhuǎn)抓圖片......

  URLLIB2文檔:http://docs.python.org/library/urllib2.html

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

  #!/usr/bin/python

  # -*- coding:utf-8 -*-

  # urllib2_test.py

  import urllib,urllib2,cookielib,socket

  url = "http://www.testurl....." #change yourself

  #最簡單方式

  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():

  #可以設置超時

  socket.setdefaulttimeout(5)

  #可以加入?yún)?shù) [無參數(shù),使用get,以下這種方式,使用post]

  params = {"wd":"a","b":"2"}

  #可以加入請求頭信息,以便識別

  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后,還可以進行其他添加,若是key重復,后者生效

python課程免費試聽預約

  • 地區(qū):
  • 姓名:
  • 手機:

  #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)

  #此句設置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 次以后最后頁面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()

責編:hym
  • 會計考試
  • 建筑工程
  • 職業(yè)資格
  • 醫(yī)藥考試
  • 外語考試
  • 學歷考試
息烽县| 德惠市| 徐汇区| 沂水县| 怀集县| 黄骅市| 万宁市| 仙居县| 千阳县| 田东县| 滕州市| 墨脱县| 临湘市| 中卫市| 莲花县| 当雄县| 天津市| 密山市| 南通市| 聂拉木县| 杂多县| 拉孜县| 张掖市| 墨玉县| 扎囊县| 昭觉县| 海口市| 辽阳县| 葫芦岛市| 清徐县| 宁明县| 义马市| 香港 | 诸暨市| 日喀则市| 仪征市| 扶余县| 鸡泽县| 浙江省| 夏邑县| 曲水县|