10个python爬虫入门实例(小结)


Posted in Python onNovember 01, 2020

昨天带伙伴萌学习python爬虫,准备了几个简单的入门实例

涉及主要知识点:

  1. web是如何交互的
  2. requests库的get、post函数的应用
  3. response对象的相关函数,属性
  4. python文件的打开,保存

代码中给出了注释,并且可以直接运行哦

如何安装requests库(安装好python的朋友可以直接参考,没有的,建议先装一哈python环境)

windows用户,Linux用户几乎一样:

打开cmd输入以下命令即可,如果python的环境在C盘的目录,会提示权限不够,只需以管理员方式运行cmd窗口

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple requests

Linux用户类似(ubantu为例): 权限不够的话在命令前加入sudo即可

sudo pip install -i https://pypi.tuna.tsinghua.edu.cn/simple requests

1.爬取强大的BD页面,打印页面信息

# 第一个爬虫示例,爬取百度页面

import requests #导入爬虫的库,不然调用不了爬虫的函数

response = requests.get("http://www.baidu.com") #生成一个response对象

response.encoding = response.apparent_encoding #设置编码格式

print("状态码:"+ str( response.status_code ) ) #打印状态码

print(response.text)#输出爬取的信息

2.常用方法之get方法实例,下面还有传参实例

# 第二个get方法实例

import requests #先导入爬虫的库,不然调用不了爬虫的函数

response = requests.get("http://httpbin.org/get") #get方法

print( response.status_code ) #状态码

print( response.text )

3. 常用方法之post方法实例,下面还有传参实例

# 第三个 post方法实例

import requests #先导入爬虫的库,不然调用不了爬虫的函数

response = requests.post("http://httpbin.org/post") #post方法访问

print( response.status_code ) #状态码

print( response.text )

4. put方法实例

# 第四个 put方法实例

import requests #先导入爬虫的库,不然调用不了爬虫的函数

response = requests.put("http://httpbin.org/put") # put方法访问

print( response.status_code ) #状态码

print( response.text )

5.常用方法之get方法传参实例(1)

如果需要传多个参数只需要用&符号连接即可如下

# 第五个 get传参方法实例

import requests #先导入爬虫的库,不然调用不了爬虫的函数

response = requests.get("http://httpbin.org/get?name=hezhi&age=20") # get传参

print( response.status_code ) #状态码

print( response.text )

6.常用方法之get方法传参实例(2)

params用字典可以传多个

# 第六个 get传参方法实例

import requests #先导入爬虫的库,不然调用不了爬虫的函数

data = {
	"name":"hezhi",
	"age":20
}
response = requests.get( "http://httpbin.org/get" , params=data ) # get传参

print( response.status_code ) #状态码

print( response.text )

7.常用方法之post方法传参实例(2) 和上一个有没有很像

# 第七个 post传参方法实例

import requests #先导入爬虫的库,不然调用不了爬虫的函数

data = {
	"name":"hezhi",
	"age":20
}
response = requests.post( "http://httpbin.org/post" , params=data ) # post传参

print( response.status_code ) #状态码

print( response.text )

8.关于绕过反爬机制,以zh爸爸为例

# 第好几个方法实例

import requests #先导入爬虫的库,不然调用不了爬虫的函数

response = requests.get( "http://www.zhihu.com") #第一次访问知乎,不设置头部信息

print( "第一次,不设头部信息,状态码:"+response.status_code )# 没写headers,不能正常爬取,状态码不是 200

#下面是可以正常爬取的区别,更改了User-Agent字段

headers = {

		"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36"

}#设置头部信息,伪装浏览器

response = requests.get( "http://www.zhihu.com" , headers=headers ) #get方法访问,传入headers参数,

print( response.status_code ) # 200!访问成功的状态码

print( response.text )

9.爬取信息并保存到本地,因为目录关系,在D盘建立了一个叫做爬虫的文件夹,然后保存信息

注意文件保存时的encoding设置

# 爬取一个html并保存

import requests

url = "http://www.baidu.com"

response = requests.get( url )

response.encoding = "utf-8" #设置接收编码格式

print("\nr的类型" + str( type(response) ) )

print("\n状态码是:" + str( response.status_code ) )

print("\n头部信息:" + str( response.headers ) )

print( "\n响应内容:" )

print( response.text )

#保存文件
file = open("D:\\爬虫\\baidu.html","w",encoding="utf") #打开一个文件,w是文件不存在则新建一个文件,这里不用wb是因为不用保存成二进制

file.write( response.text )

file.close()

10.爬取图片,保存到本地

#保存百度图片到本地

import requests #先导入爬虫的库,不然调用不了爬虫的函数

response = requests.get("https://www.baidu.com/img/baidu_jgylogo3.gif") #get方法的到图片响应

file = open("D:\\爬虫\\baidu_logo.gif","wb") #打开一个文件,wb表示以二进制格式打开一个文件只用于写入

file.write(response.content) #写入文件

file.close()#关闭操作,运行完毕后去你的目录看一眼有没有保存成功

到此这篇关于10个python爬虫入门实例(小结)的文章就介绍到这了,更多相关python爬虫入门内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
详解Python中的动态属性和特性
Apr 07 Python
详解pyqt5 动画在QThread线程中无法运行问题
May 05 Python
Python中GeoJson和bokeh-1的使用讲解
Jan 03 Python
详解Python用户登录接口的方法
Apr 17 Python
python安装scipy的方法步骤
Jun 26 Python
Python的bit_length函数来二进制的位数方法
Aug 27 Python
Django Form and ModelForm的区别与使用
Dec 06 Python
python turtle工具绘制四叶草的实例分享
Feb 14 Python
django 利用Q对象与F对象进行查询的实现
May 15 Python
浅谈keras中的Merge层(实现层的相加、相减、相乘实例)
May 23 Python
PIL.Image.open和cv2.imread的比较与相互转换的方法
Jun 03 Python
python单元测试框架pytest的使用示例
Oct 07 Python
利用pipenv和pyenv管理多个相互独立的Python虚拟开发环境
Nov 01 #Python
Python经纬度坐标转换为距离及角度的实现
Nov 01 #Python
详解Anaconda安装tensorflow报错问题解决方法
Nov 01 #Python
python Cartopy的基础使用详解
Nov 01 #Python
Python中使用aiohttp模拟服务器出现错误问题及解决方法
Oct 31 #Python
关于python中导入文件到list的问题
Oct 31 #Python
python批量检查两个对应的txt文件的行数是否一致的实例代码
Oct 31 #Python
You might like
Apache 配置详解(最好的APACHE配置教程)
2010/07/04 PHP
PHP 使用header函数设置HTTP头的示例解析 表头
2013/06/17 PHP
php文件压缩之PHPZip类用法实例
2015/06/18 PHP
php验证邮箱和ip地址最简单方法汇总
2015/10/30 PHP
php禁用函数设置及查看方法详解
2016/07/25 PHP
php 使用 __call实现重载功能示例
2019/11/18 PHP
PHP实现chrome表单请求数据转换为接口使用的json数据
2021/03/04 PHP
Prototype使用指南之ajax
2007/01/10 Javascript
js利用prototype调用Array的slice方法示例
2014/06/09 Javascript
JavaScript使用focus()设置焦点失败的解决方法
2014/09/03 Javascript
WEB前端设计师常用工具集锦
2014/12/09 Javascript
window.onload与$(document).ready()的区别分析
2015/05/30 Javascript
详解AngularJS中的表达式使用
2015/06/16 Javascript
cocos2dx骨骼动画Armature源码剖析(一)
2015/09/08 Javascript
javascript跑马灯抽奖实例讲解
2020/04/17 Javascript
Javascript类型转换的规则实例解析
2016/02/23 Javascript
Bootstrap分页插件之Bootstrap Paginator实例详解
2016/10/15 Javascript
js实现选项卡内容切换以及折叠和展开效果【推荐】
2017/01/08 Javascript
javascript 实现文本使用省略号替代(超出固定高度的情况)
2017/02/21 Javascript
在vue项目中安装使用Mint-UI的方法
2017/12/27 Javascript
浅谈Vue内置component组件的应用场景
2018/03/27 Javascript
vue拦截器实现统一token,并兼容IE9验证功能
2018/04/26 Javascript
vue3.0 搭建项目总结(详细步骤)
2019/05/20 Javascript
js tab栏切换代码实例解析
2019/09/03 Javascript
python 中文件输入输出及os模块对文件系统的操作方法
2018/08/27 Python
python实现从pdf文件中提取文本,并自动翻译的方法
2018/11/28 Python
Python3+Requests+Excel完整接口自动化测试框架的实现
2019/10/11 Python
python分别打包出32位和64位应用程序
2020/02/18 Python
德国家具购物网站:Möbel Höffner
2019/08/26 全球购物
J2EE模式面试题
2016/10/11 面试题
家长写给孩子的评语
2014/04/18 职场文书
文明市民先进事迹
2014/05/15 职场文书
捐书倡议书
2014/08/29 职场文书
世界环境日活动总结
2015/02/11 职场文书
三国演义读书笔记
2015/06/25 职场文书
导游词之千岛湖
2019/09/23 职场文书