zoukankan      html  css  js  c++  java
  • 寒假学习进度-7(Python爬虫)

    1.使用Python自带的urllib爬取一个网页的代码

    # -*- coding: UTF-8 -*-
    
    from urllib import request
    
    if __name__ == "__main__":
        response = request.urlopen("https://www.cnblogs.com/")
        html = response.read()
        html = html.decode("utf-8")
        print(html)

    通过request的URLopen向https://www.cnblogs.com/发送请求,返回的数据保存在response中

    html.decode("utf-8”)对返回的数据进行解码(decode)

    通过pip install chardet命令下载chatdet,通过chardet这个第三方库可以自动获取目标网页的编码

    # -*- coding: UTF-8 -*-
    from urllib import request
    import chardet
    
    if __name__ == "__main__":
        response = request.urlopen("https://www.cnblogs.com/")
        html = response.read()
        charset = chardet.detect(html)
        print(charset)

    urllib是学习python爬虫需要掌握的最基本的库,它主要包含四个模块:

    • urllib.request基本的HTTP请求模块。可以模拟浏览器向目标服务器发送请求。
    • urllib.error 异常处理模块。如果出现错误,可以捕捉异常。
    • urllib.parse 工具模块。提供URL处理方法, 比如对URL进行编码和解码。
    • urllib.robotpaser 用来判断哪些网站可以爬,哪些网站不可以爬。

    抓取标签

    from urllib.request import urlopen
    from urllib.error import HTTPError
    from bs4 import BeautifulSoup
    
     def getTitle(url):
        try:
            html = urlopen(url)
        except HTTPError as e:
            return None
        try:
            bs0bj = BeautifulSoup(html.read(), "html.parser")
            title = bs0bj.head.title
        except AttributeError as e:
            return None
        return title
    
    title = getTitle("http://www.baidu.com")
    if title == None:
        print("Title could not be found !")
    else:
        print(title)
  • 相关阅读:
    windows程序设计第4章Text Output练习(831121)
    约瑟夫问题的递归公式
    哈希(hash)以及C++标准库哈希(std::hash)
    返回顶部的一段代码
    对于使用 UNIKON ALL 中表的顺序
    正则表达式的实际运用
    json.help
    省市区联动
    一个JS时间选择控件
    (转)C# Enum,Int,String的互相转换 枚举转换
  • 原文地址:https://www.cnblogs.com/liujinxin123/p/12258385.html
Copyright © 2011-2022 走看看