• python爬虫模块理解


    Url管理器:

      用来管理要抓取的url和已抓取的url,防止重复抓取和循环抓取,url管理器的五个最小功能:

        1、添加url到容器中

        2、获取一个url

        3、判断url是否已在容器中

        4、判断是否还有待爬取的url

        5、将待爬取的url移到已爬取的url

    网页下载器:

      网页下载器是爬虫的核心组件,它将url对应的互联网网页已html的形式保存在本地。目前有两种网页下载器,1:urllib2(python基础模块) 2:requests(第三库)

      urllib2三种下载网页的方法:

        1、简单方式下载

        2、添加data和http header

        3、添加特殊场景的处理器

    import http.cookiejar
    import urllib.request

    url = "http://www.baidu.com"

    print("one")
    response1 = urllib.request.urlopen(url)
    print(response1.getcode())
    print (len(response1.read()))

    print("two")
    request = urllib.request.Request(url)
    request.add_header("user-agent","Mozilla/5.0")
    response2 = urllib.request.urlopen(request)
    print(response2.getcode())
    print (len(response2.read()))

    print("three")
    cj = http.cookiejar.CookieJar()
    opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
    urllib.request.install_opener(opener)
    response3 = urllib.request.urlopen(url)
    print(response3.getcode())
    print(response3.read())

    网页解析器:

      从网页中提取有价值数据的工具,常用的网页解析器有以下三种,1、正则表达式,2、html.parser(自带的模块),3、beautiful Soup(第三方的插件),4、lxml;前面一种是基于字符串的解析,后面的三种是基于一种机构化的解析(DOM)。

    response1 = urllib.request.urlopen(url)
    print(response1.getcode())
    print (len(response1.read()))
  • 相关阅读:
    前端面试题
    js collection
    javascript变量声明提升(hoisting)
    css3动画
    神奇的meta
    wap站bug小结
    前端collection
    js拾遗
    prototype之初印象
    自定义scrollBottom的值
  • 原文地址:https://www.cnblogs.com/niuyg928/p/12237655.html
Copyright © 2020-2023  润新知