您的位置:首页 > 编程语言 > Python开发

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

2019-12-03 12:16 821 查看

本文实例讲述了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程序设计有所帮助。

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息