Python大数据之网络爬虫的post请求、get请求区别实例分析

yipeiwu_com5年前Python爬虫

本文实例讲述了Python大数据之网络爬虫的post请求、get请求区别。分享给大家供大家参考,具体如下:

在JetBrains PyCharm 2016.3软件中编写代码前,需要指定python和编码方式:

#!user/bin/python

编码方式 :#coding=utf-8 或者 #-*-coding:utf-8-*-

post请求:

#导入工具,内置的库
import urllib
import urllib2
#加一个\可以换行
#response = \
  #urllib2.urlopen("https://hao.360.cn/?wd_xp1")
#print response.read()
request = urllib2.Request('http://www.baidu.com')
#response = urllib2.urlopen(request)
#构造post请求
params={}
params['account']='jredu'
params['pwd']=''
#对数据进行编码
data = urllib.urlencode(params)
response = urllib2.urlopen(request,data)
print response.url
print response.code
print response.read()

get请求:

#导入工具,内置的库
import urllib
import urllib2
#加一个\可以换行
#response = \
  #urllib2.urlopen("https://hao.360.cn/?wd_xp1")
#print response.read()
url='http://www.baidu.com'
#response = urllib2.urlopen(request)
#构造post请求
params={}
params['account']='jredu'
params['pwd']=''
#对数据进行编码
data = urllib.urlencode(params)
request = urllib2.Request(url+"?"+data)
response = urllib2.urlopen(request)
print response.url
print response.code
print response.read()

更多关于Python相关内容可查看本站专题:《Python Socket编程技巧总结》、《Python正则表达式用法总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总

希望本文所述对大家Python程序设计有所帮助。

相关文章

对python抓取需要登录网站数据的方法详解

scrapy.FormRequest login.py class LoginSpider(scrapy.Spider): name = 'login_spider' start...

scrapy爬虫完整实例

本文主要通过实例介绍了scrapy框架的使用,分享了两个例子,爬豆瓣文本例程 douban 和图片例程 douban_imgs ,具体如下。 例程1: douban 目录树 doub...

Python实现抓取城市的PM2.5浓度和排名

Python实现抓取城市的PM2.5浓度和排名

主机环境:(Python2.7.9 / Win8_64 / bs4) 利用BeautifulSoup4来抓取 www.pm25.com 上的PM2.5数据,之所以抓取这个网站,是因为上面...

Python爬虫PyQuery库基本用法入门教程

Python爬虫PyQuery库基本用法入门教程

本文实例讲述了Python爬虫PyQuery库基本用法。分享给大家供大家参考,具体如下: PyQuery库也是一个非常强大又灵活的网页解析库,如果你有前端开发经验的,都应该接触过jQue...

python爬虫面试宝典(常见问题)

是否了解线程的同步和异步? 线程同步:多个线程同时访问同一资源,等待资源访问结束,浪费时间,效率低 线程异步:在访问资源时在空闲等待时同时访问其他资源,实现多线程机制 是否...