zoukankan      html  css  js  c++  java
  • 寒假自学(十)

    希望所有温柔又可爱的人最后都能幸福❤

    今日总结:

    代码量 400行
    博客量 一篇
    所学时间 6小时左右
    了解到的知识点 python爬虫实例、Acwing每日一题

    明日计划:

    早上 python爬取疫情信息
    下午 python爬取疫情信息
    晚上 Acwing每日一题

    具体内容:
    获取丁香园疫情数据

    # 导入模块
    import requests
    
    # 发送请求,获取响应
    response = requests.get("http://ncov.dxy.cn/ncovh5/view/pneumonia")
    # 从响应中,获取数据
    # print(response.text)
    print(response.content.decode())
    

    Beautiful Soup是一个可以从HTML或XML文件提取数据的python库

    Beautiful Soup对象:代表要解析的整个文档树,它支持遍历文档树和搜索文档树中描述的大部分的方法

    from bs4 import BeautifulSoup
    
    soup = BeautifulSoup("<html>data</html>","lxml")
    print(soup)
    

    指定解析器"lxml"

    Beautiful Soup对象的find方法

    作用:搜索文档树

    参数:

    • name:标签名
    • attrs:属性字典
    • recursive:是否递归循环查找
    • text:根据文本内容查找

    返回:

    • 返回第一个元素的对象
    import requests
    from bs4 import BeautifulSoup
    
    response = requests.get("http://ncov.dxy.cn/ncovh5/view/pneumonia")
    home_page = response.content.decode()
    
    # print(home_page)
    
    soup = BeautifulSoup(home_page, 'lxml')
    script = soup.find(id='getListByCountryTypeService2true')
    text = script.string
    print(text)
    

    正则表达式是一种字符串匹配模式

    import re
    
    rs = re.findall('abc', 'abc')
    rs = re.findall('a.c', 'a%c')
    rs = re.findall('a.c', 'abc')
    rs = re.findall('a.c', 'a
    c')
    rs = re.findall('a.c', 'a.c')
    rs = re.findall('a[bc]d', 'abd')
    # 预定义的字符集
    rs = re.findall('d', '123')
    rs = re.findall('w', 'AZ123中文')
    # 数量词
    rs = re.findall('ad*', 'a123')
    rs = re.findall('ad+', 'a123')
    rs = re.findall('ad?', 'a123')
    rs = re.findall('ad{2}','a123')
    
    print(rs)
    
    
    1. .匹配除换行符 以外的所有字符
    2. d匹配[0-9]的数字
    3. w匹配字母数字_和中文
    4. *前面的一个匹配模式出现0次或多次
    5. +前面的一个匹配模式出现1次或多次
    6. 前面的一个匹配模型出现0或1次

    re.findall()方法

    API:.

    参数:

    • pattern:正则表达式
    • string:从那个字符串中查找
    • flags:匹配模式

    返回:

    • 返回string中与pattern匹配的结果列表
    import re
    
    rs = re.findall('d+', 'xiaoson1gyue2')
    
    print(rs)
    
    rs = re.findall('a.bc', 'a
    bc', re.DOTALL)
    rs = re.findall('a.bc', 'a
    bc', re.S)
    
    print(rs)
    
    import re
    
    rs = re.findall('d+', 'xiaoson1gyue2')
    
    print(rs)
    
    rs = re.findall('a.bc', 'a
    bc', re.DOTALL)
    rs = re.findall('a.bc', 'a
    bc', re.S)
    
    print(rs)
    # findall方法种分组的使用
    rs = re.findall('a(.+)bc', 'a
    bc', re.DOTALL)
    print(rs)
    
    import re
    
    rs = re.findall(r'a
    bc', 'a
    bc')
    print(rs)
    
    import requests
    import re
    from bs4 import BeautifulSoup
    
    response = requests.get("http://ncov.dxy.cn/ncovh5/view/pneumonia")
    home_page = response.content.decode()
    
    # print(home_page)
    
    soup = BeautifulSoup(home_page, 'lxml')
    script = soup.find(id='getListByCountryTypeService2true')
    text = script.string
    # print(text)
    # 使用正则表达式提取字符串
    json_str = re.findall(r'[.+]', text)[0]
    print(json_str)
    

    json模块是python自带的模块,用于jsonpython数据之间的相互转换

    import json
    
    json_str = '''[{"provinceName":"美国", "currentConfirmedCount":1179041,"confirmedCount":1643499},
    {"provinceName":"英国", "currentConfirmedCount":222227,"confirmedCount":259559}]'''
    rs = json.loads(json_str)
    
    json_str = json.dumps(rs, ensure_ascii=False)
    print(json_str)
    
    import requests
    import re
    import json
    from bs4 import BeautifulSoup
    
    response = requests.get("http://ncov.dxy.cn/ncovh5/view/pneumonia")
    home_page = response.content.decode()
    
    # print(home_page)
    
    soup = BeautifulSoup(home_page, 'lxml')
    script = soup.find(id='getListByCountryTypeService2true')
    text = script.string
    # print(text)
    # 使用正则表达式提取字符串
    json_str = re.findall(r'[.+]', text)[0]
    # print(json_str)
    
    # 把json字符串转换为python类型的数据
    last_day_corona_virus = json.loads(json_str)
    print(last_day_corona_virus)
    

    https://www.acwing.com/problem/content/758/

    因为方向是按照顺序来的,右、下、左、上、右、下...所以直接模拟即可。

    #include <bits/stdc++.h>
    using namespace std;
    int ans[110][110];
    int main()
    {
        int n,m;
        cin>>n>>m;
        int x = 1 , y = 1 , tt = 0;
        for (int i = 1; i <= n*m ; i ++)
        {
            ans[x][y] = i;
            if (tt == 0 && (ans[x][y + 1] != 0 || y == m)) tt = (tt + 1) % 4;
            else if (tt == 1 && (ans[x + 1][y] != 0 || x == n)) tt = (tt + 1) % 4;
            else if (tt == 2 && (ans[x][y - 1] != 0 || y == 1)) tt = (tt + 1) % 4;
            else if (tt == 3 && ans[x - 1][y] != 0) tt = (tt + 1) % 4;
            if (tt == 0) y += 1;
            else if (tt == 1) x += 1;
            else if (tt == 2) y -= 1;
            else x -= 1;
        }
        for (int i = 1; i <= n; i ++)
        {
            for (int j = 1; j <= m; j ++) 
            {
                cout<<ans[i][j]<<" ";
            }
            cout<<endl;
        }
           
    }
    
  • 相关阅读:
    组合与封装
    继承与派生
    面向对象编程
    subprocess、re、logging模块
    json、pickle、collections、openpyxl模块
    python内置模块
    递归函数与模块
    生成式、面向过程、与函数式
    叠加装饰器与迭代器
    闭包函数与装饰器
  • 原文地址:https://www.cnblogs.com/125418a/p/14280175.html
Copyright © 2011-2022 走看看