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

yipeiwu_com6年前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进阶之使用selenium爬取淘宝商品信息功能示例

本文实例讲述了Python进阶之使用selenium爬取淘宝商品信息功能。分享给大家供大家参考,具体如下: # encoding=utf-8 __author__ = 'Jonny'...

python抓取京东商城手机列表url实例代码

复制代码 代码如下:#-*- coding: UTF-8 -*-'''Created on 2013-12-5 @author: good-temper''' import urllib...

Python开发实例分享bt种子爬虫程序和种子解析

看到网上也有开源的代码,这不,我拿来进行了二次重写,呵呵,上代码:  #encoding: utf-8     &n...

Python爬虫实现使用beautifulSoup4爬取名言网功能案例

本文实例讲述了Python爬虫实现使用beautifulSoup4爬取名言网功能。分享给大家供大家参考,具体如下: 爬取名言网top10标签对应的名言,并存储到mysql中,字段(名言,...

python爬取淘宝商品销量信息

python爬取淘宝商品销量的程序,运行程序,输入想要爬取的商品关键词,在代码中的‘###'可以进一步约束商品的属性,比如某某作者的书籍,可以在###处输入作者名字,以及时期等等。最后可...