zoukankan      html  css  js  c++  java
  • pandas之read_html爬虫

    Pandas之read_html爬虫

    一.简介

    我们常用的爬虫工具就是urllibrequests.但是我们还没有用过pandas.read_html来爬虫吧,但是他只能爬取table属性内容table,因此功能有所局限.接下来我们分别使用上述方法来实现,来对比一下效果

    二.requests爬取

    import requests
    from lxml import etree
    import pandas as pd
    
    headers={
        "User-Agent":'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36'
    }
    
    url="https://www.kuaidaili.com/free/inha/1/"
    
    response=requests.get(url,headers=headers)
    response.encoding='utf-8'
    html=response.text
    root=etree.HTML(html)
    
    Ip_list=root.xpath('//td[@data-title="IP"]/text()')
    Port_list=root.xpath('//td[@data-title="PORT"]/text()')
    Anonymity_list=root.xpath('//td[@data-title="匿名度"]/text()')
    Types_list=root.xpath('//td[@data-title="类型"]/text()')
    Locate_list=root.xpath('//td[@data-title="位置"]/text()')
    Respondingspeed_list=root.xpath('//td[@data-title="响应速度"]/text()')
    ValidTime_list=root.xpath('//td[@data-title="最后验证时间"]/text()')
    
    df=pd.DataFrame()
    
    df["IP"]=Ip_list
    df["PORT"]=Port_list
    df["匿名度"]=Anonymity_list
    df["类型"]=Types_list
    df["位置"]=Locate_list
    df["响应速度"]=Respondingspeed_list
    df["最后验证时间"]=ValidTime_list
    
    df
    
    IP PORT 匿名度 类型 位置 响应速度 最后验证时间
    0 163.204.240.35 9999 高匿名 HTTP 广东省汕尾市 联通 1秒 2020-04-24 09:31:01
    1 113.195.16.66 9999 高匿名 HTTP 江西省九江市 联通 3秒 2020-04-24 08:31:01
    2 27.43.188.27 9999 高匿名 HTTP 广东省揭阳市 联通 1秒 2020-04-24 07:31:02
    3 113.208.115.190 8118 高匿名 HTTP 北京市朝阳区 北京商务中心区通信科技有限公司 电信 0.4秒 2020-04-24 06:31:01
    4 125.110.100.170 9000 高匿名 HTTP 浙江省温州市 电信 0.8秒 2020-04-24 05:31:01
    5 1.198.72.19 9999 高匿名 HTTP 河南省济源市 电信 3秒 2020-04-24 04:31:01
    6 121.232.199.174 9000 高匿名 HTTP 江苏省镇江市 电信 3秒 2020-04-24 03:31:01
    7 106.110.65.16 8118 高匿名 HTTP 江苏省徐州市 电信 2秒 2020-04-24 02:31:01
    8 163.204.246.152 9999 高匿名 HTTP 广东省汕尾市 联通 2秒 2020-04-24 01:31:02
    9 60.168.207.82 1133 高匿名 HTTP 安徽省合肥市 电信 3秒 2020-04-24 00:31:01
    10 183.166.21.218 9999 高匿名 HTTP 安徽省淮南市 电信 3秒 2020-04-23 23:31:01
    11 122.243.13.206 9000 高匿名 HTTP 浙江省金华市 电信 0.5秒 2020-04-23 22:31:01
    12 110.243.2.66 9999 高匿名 HTTP 河北省唐山市 联通 0.9秒 2020-04-23 21:31:01
    13 171.13.203.95 9999 高匿名 HTTP 河南省鹤壁市 电信 3秒 2020-04-23 20:31:01
    14 163.204.245.179 9999 高匿名 HTTP 广东省汕尾市 联通 2秒 2020-04-23 19:31:02
    df.to_csv("free_proxy1.csv",mode="a+",header=1,index=0,encoding="utf-8")
    

    如果要实现多页爬虫,则设置一个for循环就可:

    
    def Get_data(url):
        response=requests.get(url,headers=headers)
        response.encoding='utf-8'
        html=response.text
        root=etree.HTML(html)
        
        return root
    
    def Parse_data(root):
        
        Ip_list=root.xpath('//td[@data-title="IP"]/text()')
        Port_list=root.xpath('//td[@data-title="PORT"]/text()')
        Anonymity_list=root.xpath('//td[@data-title="匿名度"]/text()')
        Types_list=root.xpath('//td[@data-title="类型"]/text()')
        Locate_list=root.xpath('//td[@data-title="位置"]/text()')
        Respondingspeed_list=root.xpath('//td[@data-title="响应速度"]/text()')
        ValidTime_list=root.xpath('//td[@data-title="最后验证时间"]/text()')
        
        df=pd.DataFrame()
    
        df["IP"]=Ip_list
        df["PORT"]=Port_list
        df["匿名度"]=Anonymity_list
        df["类型"]=Types_list
        df["位置"]=Locate_list
        df["响应速度"]=Respondingspeed_list
        df["最后验证时间"]=ValidTime_list
        
        return df
        
        df.to_csv("free_proxy2.csv",mode="a+",header=None,index=None,encoding="utf-8")
        
    def main():
        
        start_page=int(input("开始页面(1<=):"))
        end_page=int(input("结束页面(1<=):"))
        
        base_url="https://www.kuaidaili.com/free/inha/{}/"
        
        for i in range(start_page,end_page+1):
            
            print("解析第{}页".format(i))
            
            url=base_url.format(i)
            
            root=Get_data(url)
            df=Parse_data(root)
            
            df.to_csv("free_proxy2.csv",mode="a+",header=1,index=0,encoding="utf-8")
            
    if __name__=="__main__":
        
        main()
    
    
    开始页面(1<=): 1
    结束页面(1<=): 2
    
    
    解析第1页
    解析第2页
    

    三.read_html爬取

    import pandas as pd
    
    url="https://www.kuaidaili.com/free/inha/1/"
    
    df=pd.read_html(url,encoding="utf-8")[0] # [0]:表示第一个table,多个table需要指定,如果不指定默认第一个
    
    df.to_csv("free_proxy3.csv",mode="a+",header=1,index=0,encoding="utf-8")
    
    df
    
    IP PORT 匿名度 类型 位置 响应速度 最后验证时间
    0 175.43.151.48 9999 高匿名 HTTP 福建省泉州市 联通 1秒 2020-04-24 11:31:01
    1 1.193.245.3 9999 高匿名 HTTP 河南省鹤壁市 电信 1秒 2020-04-24 10:31:02
    2 163.204.240.35 9999 高匿名 HTTP 广东省汕尾市 联通 1秒 2020-04-24 09:31:01
    3 113.195.16.66 9999 高匿名 HTTP 江西省九江市 联通 3秒 2020-04-24 08:31:01
    4 27.43.188.27 9999 高匿名 HTTP 广东省揭阳市 联通 1秒 2020-04-24 07:31:02
    5 113.208.115.190 8118 高匿名 HTTP 北京市朝阳区 北京商务中心区通信科技有限公司 电信 0.4秒 2020-04-24 06:31:01
    6 125.110.100.170 9000 高匿名 HTTP 浙江省温州市 电信 0.8秒 2020-04-24 05:31:01
    7 1.198.72.19 9999 高匿名 HTTP 河南省济源市 电信 3秒 2020-04-24 04:31:01
    8 121.232.199.174 9000 高匿名 HTTP 江苏省镇江市 电信 3秒 2020-04-24 03:31:01
    9 106.110.65.16 8118 高匿名 HTTP 江苏省徐州市 电信 2秒 2020-04-24 02:31:01
    10 163.204.246.152 9999 高匿名 HTTP 广东省汕尾市 联通 2秒 2020-04-24 01:31:02
    11 60.168.207.82 1133 高匿名 HTTP 安徽省合肥市 电信 3秒 2020-04-24 00:31:01
    12 183.166.21.218 9999 高匿名 HTTP 安徽省淮南市 电信 3秒 2020-04-23 23:31:01
    13 122.243.13.206 9000 高匿名 HTTP 浙江省金华市 电信 0.5秒 2020-04-23 22:31:01
    14 110.243.2.66 9999 高匿名 HTTP 河北省唐山市 联通 0.9秒 2020-04-23 21:31:01

    read_html函数的源码:

    def read_html(
        io,
        match=".+",
        flavor=None,
        header=None,
        index_col=None,
        skiprows=None,
        attrs=None,
        parse_dates=False,
        thousands=",",
        encoding=None,
        decimal=".",
        converters=None,
        na_values=None,
        keep_default_na=True,
        displayed_only=True,
    ):
        r"""
        Read HTML tables into a ``list`` of ``DataFrame`` objects.
    
        Parameters
        ----------
        io : str, path object or file-like object
            A URL, a file-like object, or a raw string containing HTML. Note that
            lxml only accepts the http, ftp and file url protocols. If you have a
            URL that starts with ``'https'`` you might try removing the ``'s'``.
    
        match : str or compiled regular expression, optional
            The set of tables containing text matching this regex or string will be
            returned. Unless the HTML is extremely simple you will probably need to
            pass a non-empty string here. Defaults to '.+' (match any non-empty
            string). The default value will return all tables contained on a page.
            This value is converted to a regular expression so that there is
            consistent behavior between Beautiful Soup and lxml.
    
        flavor : str or None
            The parsing engine to use. 'bs4' and 'html5lib' are synonymous with
            each other, they are both there for backwards compatibility. The
            default of ``None`` tries to use ``lxml`` to parse and if that fails it
            falls back on ``bs4`` + ``html5lib``.
    
        header : int or list-like or None, optional
            The row (or list of rows for a :class:`~pandas.MultiIndex`) to use to
            make the columns headers.
    
        index_col : int or list-like or None, optional
            The column (or list of columns) to use to create the index.
    
        skiprows : int or list-like or slice or None, optional
            Number of rows to skip after parsing the column integer. 0-based. If a
            sequence of integers or a slice is given, will skip the rows indexed by
            that sequence.  Note that a single element sequence means 'skip the nth
            row' whereas an integer means 'skip n rows'.
    
        attrs : dict or None, optional
            This is a dictionary of attributes that you can pass to use to identify
            the table in the HTML. These are not checked for validity before being
            passed to lxml or Beautiful Soup. However, these attributes must be
            valid HTML table attributes to work correctly. For example, ::
    
                attrs = {'id': 'table'}
    
            is a valid attribute dictionary because the 'id' HTML tag attribute is
            a valid HTML attribute for *any* HTML tag as per `this document
            <http://www.w3.org/TR/html-markup/global-attributes.html>`__. ::
    
                attrs = {'asdf': 'table'}
    
            is *not* a valid attribute dictionary because 'asdf' is not a valid
            HTML attribute even if it is a valid XML attribute.  Valid HTML 4.01
            table attributes can be found `here
            <http://www.w3.org/TR/REC-html40/struct/tables.html#h-11.2>`__. A
            working draft of the HTML 5 spec can be found `here
            <http://www.w3.org/TR/html-markup/table.html>`__. It contains the
            latest information on table attributes for the modern web.
    
        parse_dates : bool, optional
            See :func:`~read_csv` for more details.
    
        thousands : str, optional
            Separator to use to parse thousands. Defaults to ``','``.
    
        encoding : str or None, optional
            The encoding used to decode the web page. Defaults to ``None``.``None``
            preserves the previous encoding behavior, which depends on the
            underlying parser library (e.g., the parser library will try to use
            the encoding provided by the document).
    
        decimal : str, default '.'
            Character to recognize as decimal point (e.g. use ',' for European
            data).
    
        converters : dict, default None
            Dict of functions for converting values in certain columns. Keys can
            either be integers or column labels, values are functions that take one
            input argument, the cell (not column) content, and return the
            transformed content.
    
        na_values : iterable, default None
            Custom NA values.
    
        keep_default_na : bool, default True
            If na_values are specified and keep_default_na is False the default NaN
            values are overridden, otherwise they're appended to.
    
        displayed_only : bool, default True
            Whether elements with "display: none" should be parsed.
    
    • io:接收网址,文件,字符串。网址不接受https,尝试去掉s后爬取
    • match:正则表达式,返回与正则表达式匹配的表格。默认".+"
    • flavor:解析器。默认为(“lxml”,“bs4”)
    • header:指定列标题所在的行
    • index_col:指定行标题对应的列
    • skiprows:跳过第n行
    • attrs:传递一个字典,标示表格的属性值
    • parse_dates:解析日期
    • thousands:千位分隔符
    • encoding:编码方式
    • decimal:小数点指示,默认使用"."
    • converters:转换某些列的函数的字典
    • na_values:标示那些为NA的值
    • keep_default_na:保持默认的NA值,与na_values一起使用
    • displayed_only:是否应解析具有"display:none"的元素。默认为True

    缺点:如果网站需要使用用户代理(User_Agent),那么read_html就无法爬取成功

    url='https://www.xicidaili.com'
    
    df=pd.read_html(url,encoding="utf-8")[0]
    

    出现错误:HTTPError: HTTP Error 503: Service Temporarily Unavailable

    三.总结

    pandas.read_html有其方便之处,它可以使用几行代码完成我们所要爬取的数据,其实质也是爬取网页数据并使用解析器进行解析文本然后存储成DataFrame格式的数据。但是对于一些有要求的网站,此函数便无法爬取。还是需要使用urllib和requests来爬取

  • 相关阅读:
    crossvcl使用
    CSS垂直居中的方法
    IIS7 启用GZip压缩
    javascript arguments解释,实现可变长参数。
    DataTable转List<dynamic>
    Bootstrap表单
    Func<T>、Action<T> 的区别于说明
    jQuery.fn.extend() 与 jQuery.extend()
    javascript this关键字指向详解
    javascript call与apply关键字的作用
  • 原文地址:https://www.cnblogs.com/LQ6H/p/12940527.html
Copyright © 2011-2022 走看看