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爬虫实例扒取2345天气预报

Python爬虫实例扒取2345天气预报

寒假里学习了一下Python爬虫,使用最简单的方法扒取需要的天气数据,对,没听错,最简单的方法。甚至没有一个函数封装。。 网址:http://tianqi.2345.com/wea_hi...

python基于BeautifulSoup实现抓取网页指定内容的方法

本文实例讲述了python基于BeautifulSoup实现抓取网页指定内容的方法。分享给大家供大家参考。具体实现方法如下: # _*_ coding:utf-8 _*_ #xiao...

python利用beautifulSoup实现爬虫

以前讲过利用phantomjs做爬虫抓网页 /post/55789.htm 是配合选择器做的 利用 beautifulSoup(文档 :http://www.crummy.com/sof...

python爬虫之百度API调用方法

python爬虫之百度API调用方法

调用百度API获取经纬度信息。 import requests import json address = input('请输入地点:') par = {'address': add...

Python爬虫爬取新浪微博内容示例【基于代理IP】

Python爬虫爬取新浪微博内容示例【基于代理IP】

本文实例讲述了Python爬虫爬取新浪微博内容。分享给大家供大家参考,具体如下: 用Python编写爬虫,爬取微博大V的微博内容,本文以女神的微博为例(爬新浪m站:https://m.w...