10个经典Python爬虫入门实例!你还不知道吗

324次阅读
没有评论
10个经典Python爬虫入门实例!你还不知道吗

伙伴们学习python爬虫,准备了几个简单的入门实例,分享给大家。

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

如何安装requests库(安装好python的朋友可以直接参考,没有的,建议先装一哈python环境)
windows用户,Linux用户几乎一样:

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

1 pip install –

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

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

1 sudo pip install –

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

  • 爬取强大的BD页面,打印页面信息
  • 1 # 第一个爬虫示例,爬取百度页面

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

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

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

    5 print(“状态码:”+ str( response.status_code ) ) #打印状态码

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

  • 常用方法之get方法实例,下面还有传参实例
  • 1 # 第二个get方法实例

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

    3 response = requests.get(“http://httpbin.org/get”) #get方法

    4 print( response.status_code ) #状态码

    5 print( response.text )

  • 常用方法之post方法实例,下面还有传参实例
  • 1 # 第三个 post方法实例

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

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

    4 print( response.status_code ) #状态码

    5 print( response.text )

  • put方法实例
  • 1 # 第四个 put方法实例

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

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

    4 print( response.status_code ) #状态码

    5 print( response.text )

  • 常用方法之get方法传参实例(1)
  • 如果需要传多个参数只需要用&符号连接即可如下:

    1# 第五个 get传参方法实例
    2 import requests #先导入爬虫的库,不然调用不了爬虫的函数

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

    4 print( response.status_code ) #状态码

    5 print( response.text )

  • 常用方法之get方法传参实例(2)
  • params用字典可以传多个

    1 # 第六个 get传参方法实例

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

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

    4 print( response.status_code ) #状态码

    5 print( response.text )

  • 常用方法之post方法传参实例(2) 和上一个有没有很像
  • 1 # 第七个 post传参方法实例

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

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

    4 print( response.status_code ) #状态码

    5 print( response.text )

  • 关于绕过反爬机制,以zh爸爸为例
  • 1 # 第好几个方法实例

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

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

    4 print( “第一次,不设头部信息,状态码:”+response.status_code )

    没写headers,不能正常爬取,状态码不是 2005 #下面是可以正常爬取的区别,更改了User-Agent字段


    6 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”

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

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

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

    10 print( response.text )

  • 爬取信息并保存到本地
    因为目录关系,在D盘建立了一个叫做爬虫的文件夹,然后保存信息
  • 注意文件保存时的encoding设置

    1 # 爬取一个html并保存

    2 import requests

    3 url = “http://www.baidu.com”

    4 response = requests.get( url )

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

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

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

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

    9 print( “n响应内容:” )

    10 print( response.text )

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

    12 file.write( response.text )

    13 file.close()

  • 爬取图片,保存到本地
  • 1 #保存百度图片到本地

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

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

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

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

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

    神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

    相关文章:

    版权声明:Python教程2022-10-28发表,共计3097字。
    新手QQ群:570568346,欢迎进群讨论 Python51学习