通用爬虫模块+requests包的5种作用

网络爬虫(又被称为网页蜘蛛,网络机器人)就是模拟客户端发送网络请求,接受响应,一种按照一定的规则,自动地抓取互联网信息的程序。
爬虫分类:
通用爬虫:指搜索引擎的爬虫
聚焦爬虫:针对特定网站的爬虫
爬虫的流程:
url–>发送请求,获取响应———》提取数据———〉保存
发送请求,获取响应———》提取URL
HTTP:
超文本传输协议
默认端口号:80
HTTPS:(带加密)
HTTP+SSL(安全套接字层)
默认端口号:443
Robots协议:网站通过Robots协议告诉搜索引擎那些页面可以抓取,那些页面不能抓取。

爬虫要根据但钱url地址对应的响应为准,当前url地址的elements的内容和URL的响应不一样。

页面上的数据在哪里:
当前url地址的响应中
其他的url地址对应的响应中
比如ajax请求中
js生成
部分数据在响应中
全部通过js生成

在这里插入图片描述在这里插入图片描述
在这里插入图片描述

import requests
url="https://www.csdn.net/"
heads={"user-agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1"}
re = requests.get(url,headers=heads)
print(re.content.decode())#解码
file_path = "{}-第{}页.html"#文件存储位置
with open("file_path", "w",encoding="utf-8") as f:  
    f.write(re.content.decode())

在这里插入图片描述在这里插入图片描述当我们想往url里面放参数的时候,我们可以用\s或者上面这种方式

**

requests包的5种作用

**
requests模块发送post请求:(主要是提交数据,登陆时候使用)
在这里插入图片描述
在这里插入图片描述使用代理:

在这里插入图片描述在这里插入图片描述#使用代理ip
准备一堆的ip地址,组成ip池,随机选择一个ip来使用
如何选择代理ip
-{“ip”:ip,“time”:0}
-[{{},{},{},{}}],对这个ip的列表进行排序,按照使用次数进行排序
选择使用次数较少的10个ip,从中随机选择一个
-检查ip的可用性
可以使用requests添加参数,判断ip地址的质量
在线代理ip质量检测的网站
在这里插入图片描述在这里插入图片描述###携带cookie请求
-携带一堆cookie进行请求,把cookie组成cookie池
在这里插入图片描述#不发送post请求,使用cookie获取登陆后的页面
-cookie过期时间很长的网站
-在cookie过期之前能够拿到所有的数据,比较麻烦
-配合其他程序一起使用,其他程序专门获取cookie,当前程序专门请求页面
###字典推导式,列表推导式
cookies={i.splilt("=")[0]:i.splilt("=")[1] for i in cookies.split(";")}#字典推导式
[self.url_temp.format(i*50) for i in range(1000)]#列表推导式

获取登陆后的页面三种方式
-实例化session,使用session发送post请求,在使用他获取登陆后的页面
-headers中添加cookie键,值为cookie字符串
-在请求方法中添加cookies参数,接受字典形式的cookie.字典形式的cookie中的键是cookie的name,值是cookie的value

在这里插入图片描述在这里插入图片描述#requests中解决编解码的方法
-response.content.decode()
-response.content.decode(“gbk”)
-response.text#编解码随机猜测
在这里插入图片描述#寻找登陆的post地址
-在form表单中寻找action对应的url地址
-post的数据是input标签中name的值作为键,真正的用户名密码作为值的字典,post的url地址就是action,对应的url地址。
#抓包,寻找登陆的url地址
-勾选perserve log按钮,防止页面跳转找不到url
-寻找post数据,确定参数
-参数不会变,直接用,比如密码不是动态加密的时候
-参数会变
-在当前的响应中
-通过js生成
#定位想要的js
-选择会触发js时间的按钮,点击event listener,找到js的位置
-通过chrome中的search all file 来搜索url中关键字
-填加断点的方式查看js的操作,通过python来进行同样的操作
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200306154016920.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3poYW5sb25nMTE=,size_16,color_FFFFFF,t_70

###安装第三方模块
-pip install
-下载源码解码,进入解压后的目录,python set.py install
-’***.whl’安装方法
pip install ***.whl
在这里插入图片描述
在这里插入图片描述在这里插入图片描述
在这里插入图片描述

import requests
import json
from retrying import retry
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36"}

@retry(stop_max_attempt_number=3)#最大请求次数
def _parse_url(url,method):#data,proxies
#"函数作用是预判出现问题"
    print("*"*20)
    if method =="POST":
        response = requests.post(url, headers=headers)#,data=data,proxies=proxies
    else:
        response = requests.get(url, headers=headers, timeout=3)#,proxies=proxies
    assert response.status_code == 200
    return response.content.decode()


def parse_url(url,method ="GET",data=None,proxies={}):
    try:
        html_str = _parse_url(url,"GET")
    except:
        html_str = None
    return html_str


if __name__ == '__main__':
    url = "https://www.baidu.com/sugrec?prod=pc_his&from=pc_web&json=1&sid=30970_1446_21087_30840_30998_30824&hisdata=%5B%7B%22time%22%3A1583465630%2C%22kw%22%3A%22%E5%A4%B4%E6%9D%A1%22%7D%2C%7B%22time%22%3A1583465699%2C%22kw%22%3A%22%E4%BD%A0%E7%AC%91%E8%B5%B7%E6%9D%A5%E7%9C%9F%E5%A5%BD%E7%9C%8B%22%7D%2C%7B%22time%22%3A1583465781%2C%22kw%22%3A%22%E4%BB%8A%E6%97%A5%E5%A4%B4%E6%9D%A1%22%2C%22fq%22%3A2%7D%2C%7B%22time%22%3A1583468451%2C%22kw%22%3A%22%E8%B1%86%E7%93%A3%22%7D%5D&req=2&csor=0"
    result=parse_url(url)
    ret1=json.loads(result)
    print(ret1)
    with open("douban.json","w")as f:
        f.write(json.dumps(ret1,ensure_ascii=False,indent=4))
        #参数作用
            #ensure_ascii=False的作用是不用ascill显示
            #indent的作用是让输出结果更加好看
    with open("douban.json","r")as f:
        ret2=f.read()
        ret3=json.loads(ret2)
        print(ret3)
        print(type(ret3))
发布了16 篇原创文章 · 获赞 0 · 访问量 501

猜你喜欢

转载自blog.csdn.net/zhanlong11/article/details/104618712