美文网首页
2.爬虫基础2

2.爬虫基础2

作者: IPhone2 | 来源:发表于2018-10-24 09:13 被阅读0次

    1.post


    百度翻译
    
    表单数据处理
        # 处理数据 先变成符合url规则的字符串 然后把再把字符串转换成字节码
        bytes_data = urllib.parse.urlencode(form_data).encode()
        response = urllib.request.urlopen(request, data=bytes_data)
    

    2.ajax


    • ajax-get
      豆瓣电影排行榜
      1.分析请求字符串
      start=20&limit=20
      每页10条 显示第页如何写?
      2.发送请求
      ajax的get请求和以前的get请求一样
      只不过获取到的不再是html页面而是json数据
    • ajax-post
      肯德基店铺位置
      ajax的post请求和以前的post请求一样
      只不过获取到的不再是html页面而是json数据

    .复杂get


        page = 50 * (page - 1)
        query_string = {
            "kw": tieba,
            "pn": page
        }
        url += urllib.parse.urlencode(query_string)
        print(url)
        page_request = urllib.request.Request(url)
        只是案例复杂了一些,和昨天的带参数的get一样,只是这回需要计算分页并保存到本地
    

    4.URLError\HTTPError


    异常处理:Exception  官方的异常基类
    URLError\HTTPError是Exception的子类,在向url发送请求的时候,会出现的一些异常情况
    HTTPError是URLError的子类
    所以 如果两个异常都要捕获 要把http放上面 否则都会被URL捕获
    try:
        response = urllib.request.urlopen(url)
        print(response)
    except urllib.error.HTTPError as e:
        print("HTTP错误")
    except urllib.error.URLError as e:
        print("URL地址有误")
    

    5.Handler处理器、自定义Opener


    urllib.request.urlopen() 函数实现简单的发送请求,不能定制请求头
    引入了构建请求对象
    urllib.request.Request() 创建的request对象可以定制头部,但不能实现设置代理、携带cookie等更高级的功能
    引入了 Handler和Opener
    可以实现代理、携带cookie等高级功能
    
    import urllib.request
    import urllib.error
    
    # 1. 创建handler
    handler = urllib.request.HTTPHandler()
    # 2. 将handler传入函数获取opener
    opener = urllib.request.build_opener(handler)
    url = "http://www.baidu.com/"
    # 3. 通过open()函数来发送请求
    # response = opener.open(url)  # 可以传url字符串 也可以传request对象
    user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " \
                 "(KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"
    headers = {"User-Agent": user_agent}
    request = urllib.request.Request(url, headers=headers)
    response = opener.open(request)
    print(response)
    

    6.设置代理


    代理:作为中间人,帮助客户端(浏览器)访问其他服务器的服务器
    
    代码请求中设置代理
    
    import urllib.request
    proxy = {"http": "114.226.65.220:6666"}  # {"协议":"地址:端口"}
    # 1. 设置代理使用 ProxyHandler
    handler = urllib.request.ProxyHandler(proxy)
    # 2. 把创建的handler传入build函数 创建出opener
    opener = urllib.request.build_opener(handler)
    url = "http://www.baidu.com/s?wd=ip"  # 请求字符串
    # 客户端信息
    user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " \
                 "(KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"
    headers = {"User-Agent": user_agent}
    request = urllib.request.Request(url, headers=headers)
    # 3. 使用opener打开
    response = opener.open(request)
    # 4. 写入本地文件 以便查看效果
    with open("ip.html", "wb") as fp:
        fp.write(response.read())
    

    相关文章

      网友评论

          本文标题:2.爬虫基础2

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