首页 > 其他 > 详细

request补充和bs4的五种过滤器

时间:2020-01-02 21:33:37      阅读:99      评论:0      收藏:0      [点我收藏+]

reques补充

Response的属性

响应状态码(*******)
print(response.status_code)

响应文本(*******)
print(response.text)

响应的cookies(*******)  注意: 有些网站可以直接传入cookies=cookies对象,有些需要先转换成dict类型
print(type(response.cookies))
将cookies对象转成dict类型
print(type(response.cookies.get_dict()))
print(type(response.cookies.items()))


response = requests.get('https://www.baidu.com', headers=headers)
print(response.text)
响应编码格式(*******)
print(response.encoding)
解决字符编码不对应导致的数据展示错乱问题
response.encoding = 'utf-8'
print(response.encoding)
print(response.text)


返回的二进制数据(*******)
一般用于爬取图片、视频、音频
print(response.content)

获取当前网站的url地址
print(response.url)



获取网站返回的json数据
print(response.json())
import json

response = requests.get('https://landing.toutiao.com/api/pc/realtime_news/')
print(response.status_code)
# print(response.text)
# print(json.loads(response.text))
print(response.json())


获取图片二进制流数据,并且保存到本地
response = requests.get('https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1577767718263&di=76c4eba6804809f537fbafb2e080414b&imgtype=0&src=http%3A%2F%2Fpic1.win4000.com%2Fpic%2F7%2F84%2F183b392788.jpg')
print(response.status_code)

# 注意: 二进制数据有可能会很大
print(response.content)

with open('小泽3.jpg', 'wb') as f:
    # f.write(response.content)
    # iter_content: 将二进制数据装进一个迭代器中
    for line in response.iter_content():
        f.write(line)

bs4五种过滤器

- 字符串过滤器   字符串全局匹配
            - name = 'p'
            name 属性匹配
            attrs 属性查找匹配
            text 文本匹配

        - 正则过滤器

            re模块匹配
            - name = re.compile()
            name 属性匹配
            attrs 属性查找匹配
            text 文本匹配

        - 列表过滤器
            列表内的数据匹配

            - name = ['tank', 100]
            name 属性匹配
            attrs 属性查找匹配
            text 文本匹配

        - bool过滤器
            True匹配
            - name = True
            name 属性匹配
            attrs 属性查找匹配
            text 文本匹配

        - 方法过滤器
            用于一些要的属性以及不需要的属性查找。
            - name = func()
            name 属性匹配
            attrs 属性查找匹配
            text 文本匹配

request补充和bs4的五种过滤器

原文:https://www.cnblogs.com/chanyuli/p/12134773.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!