美文网首页
爬虫的主要内容

爬虫的主要内容

作者: 一月芷 | 来源:发表于2019-02-20 12:30 被阅读9次

    爬虫的主要内容


    requests

    • 发送请求
    • 传递url参数
    • 读取相应内容
    • 定制请求头部
    • Post请求
    • 响应状态码
    • 重定向和历史
    • 超时

    获取url

    import requests
     url = 'http://www.baidu.com'
    r= requests.get(url)
     print(r)
    <Response [200]>
    print(r.text)
    <meta http-equiv="refresh" content="0; url = https://www.baidu.com/?tn=95589872_hao_pg&ch=10"/> 
    

    传递参数

    params={'k1':'v1','k2':'v2'}
    r=requests.get('http://httpbin.org/get',params)
    print(r.url)
    http://httpbin.org/get?k1=v1&k2=v2
    

    处理二进制内容

    from io import BytesIO
    

    处理图片

    from PIL import Image
    
    image = Image.open(BytesIO(r.content)#将图片转换成二进制文件
    

    json处理

    r =requests.get('https://github.com/timeline.json')
    

    提交表单

    form = {'username':'user','password':'pass'}
    r= r.requests.post('http://www.org/post',data=form)
    print(r.text)
    

    cookie

    url ='http//ww.baidu.com'
    r=requests.get(url)
    cookies=r.cookies
    for k,v in cookies.get_dict().items():  #取出COOKIE并遍历
        print(k,v)
    

    重定向和重定向历史
    主要用于尊重

    r=requests.head('http://github.com',allow_redirects=True)
    print(r.url)
    

    相关文章

      网友评论

          本文标题:爬虫的主要内容

          本文链接:https://www.haomeiwen.com/subject/mdzcyqtx.html