个人学习爬虫笔记之urllib模块的使用一

打开某一网站(两种请求类型):


  1 import urllib.request
  2 
  3 response = urllib.request.urlopen('http://www.baidu.com') #request baidu,(type:get)
  4 print(response.read().decode('utf-8'))  #print the source webpage code of baidu

 

  1 import urllib.request
  2 import urllib.parse 
  6 
  7 data = bytes(urllib.parse.urlencode({'world': 'hello'}), encoding='utf-8')
  8 response = urllib.request.urlopen('http://httpbin.org/post', data=data)
  9 print(response.read())

超时并且抓取错误:

  1 import socket
  2 import urllib.request
  3 import urllib.parse
  4 import urllib.error
  5 
 15 
 16 try:
 17    response = urllib.request.urlopen('http://httpbin.org/post', timeout=0.1) #timeout and give
     error
 18 except urllib.error.URLError as e:
 19    if isinstance(e.reason, socket.timeout):
 20       print('TIME OUT')

响应: 

  1 import socket
  2 import urllib.request
  3 import urllib.parse
  4 import urllib.error
  5 
  6 response = urllib.request.urlopen('http://www.python.org')
  7 print(type(response))

 状态码和响应头:

  1 import socket
  2 import urllib.request
  3 import urllib.parse
  4 import urllib.error
  5 
  6 response = urllib.request.urlopen('http://www.python.org')
  7 print(response.status)
  8 print(response.getheaders())
  9 print(response.getheader('Server'))
 10 
 11 

响应体的内容:

  1 import socket
  2 import urllib.request
  3 import urllib.parse
  4 import urllib.error
  5 
  6 response = urllib.request.urlopen('http://www.python.org')
  7 print(response.read().decode('utf-8'))
  8 

request:

  1 import socket
  2 import urllib.request
  3 import urllib.parse
  4 import urllib.error
  5 
  6 request = urllib.request.Request('http://www.python.org')
  7 response = urllib.request.urlopen(request)
  8 print(response.read().decode('utf-8'))
  9 

request的整体结构: 

  1 import socket
  2 import urllib.request
  3 import urllib.parse
  4 import urllib.error
  5 from urllib import request, parse
  6 
  7 url = 'http://httpbin.org/post'
  8 headers = {
  9     'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64)',
 10     'Host': 'httpbin.org'}
 11 dict = {'name': 'Germey'}
 12 data = bytes(parse.urlencode(dict), encoding='utf-8')
 13 req = request.Request(url=url, data=data, headers=headers, method='POST')
 14 response = request.urlopen(req)
 15 print(response.read().decode('utf-8'))

利用方法添加header:

  1 import socket
  2 import urllib.request
  3 import urllib.parse
  4 import urllib.error
  5 from urllib import request, parse
  6 
  7 url = 'http://httpbin.org/post'
  8 dict = {'name': 'Germey'}
  9 data = bytes(parse.urlencode(dict), encoding='utf-8')
 10 req = request.Request(url=url, data=data, method='POST')
 11 req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; WOW64)')
 12 response = request.urlopen(req)
 13 print(response.read().decode('utf-8'))
 14 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值