zoukankan      html  css  js  c++  java
  • 4/21学习总结

    今天学习了有关python的一些相关知识

    1.requests库:实现网页的下载

    使用方法:

    import requests//引入
    def download_all_htmls():#设置一个函数
        htmls=[];
        for idx in range(43):#要下载的网页有分页 总共43页
         url=f"https://yz.chsi.com.cn/sch/?start={idx*20}"#f是为了使字符串格式化
         print("craw html",url)
         r=requests.get(url)#得到网页内容,状态码应为200,不为200则异常
         if r.status_code!=200:
            raise Exception("error")
         htmls.append(r.text)
        return htmls
    htmls=download_all_htmls()

    r=requests.get()传入一个url

    r.text得到文本形式的网页内容

    2.

    BeautifulSoup库:解析需要的标签,链接等内容
    使用方法:
    祝:仅为举例,与上面的代码并不是一部分
    def pa(html):
        soup=BeautifulSoup(html,"html.parser")
        articles=soup.find_all("artical")
        datas=[]
        for article in articles:
            title_node=(
                article.find("h2",class_="entry_title")
                .find("a")
            )
            title=title_node.get_text()
            # 得到title的内容
            link=title_node["href"]
            # 得到title的链接
            datas.append({"title":title,"link":link});
            return datas

    3.pandas库:用于数据挖掘和数据分析,同时也提供数据清洗功能。

  • 相关阅读:
    【Linux高频命令专题(7)】rm
    【Linux高频命令专题(6)】mkdir
    【mongoDB运维篇①】用户管理
    【Linux高频命令专题(5)】rmdir
    【mongoDB中级篇②】索引与expain
    【mongoDB中级篇①】游标cursor
    Lua中的字符串函数库
    ngx_lua 随笔
    Nginx与Lua
    MAC 上搭建lua
  • 原文地址:https://www.cnblogs.com/wangzhaojun1670/p/12753454.html
Copyright © 2011-2022 走看看