zoukankan      html  css  js  c++  java
  • format使用方法

    一、中文 对齐问题愿意

    :      <填充>    <对齐>    <宽度>          ,        <.精度>      <类型>

    引号    用于填充的   <左对齐   槽的设定输出    数字的千位分割符   浮点数小数部分   整数类型b,c,

    符号    单个符号    >右对齐   宽度        适用于整数和浮点   的精度或字符串   d,o,x,X浮点数

                  ^居中对齐             数          的最大输出长度   类型e,E,f,%

    当中文字符宽度不够时,采用西文字符进行填充;中西文字符占用宽度不同。

    中文字符宽度不够时解决方法:采用中文字符的空格填充chr(12288)

     1 import requests
     2 import parsel
     3 
     4 """
     5 模拟浏览器进入网页
     6 获取url的内容
     7 """
     8 def get_page_url(hd,url):
     9     try:
    10         r =requests.get(url,headers=hd,timeout=30)
    11         r.raise_for_status()    #判断返回的状态是否是200
    12         r.encoding = r.apparent_encoding    #转换编码方式为内容分析出的相应编码方式
    13         return r.text
    14     except:
    15         return None
    16 
    17 """
    18 解析页面内容
    19 使用xpath获取有用的信息
    20 """
    21 def parsing_website(html,content_list):
    22     sel = parsel.Selector(html)
    23     xp = sel.xpath("//tr[@class='alt']")
    24     # list_from = []
    25     #使用for循环获取需要的信息
    26     for i in xp:
    27         ranking= i.xpath('./td[1]/text()').get()
    28         name = i.xpath('./td[2]/div/text()').get()
    29         province = i.xpath('./td[3]/text()').get()
    30         total_score = i.xpath('./td[4]/text()').get()
    31         index = i.xpath('./td[5]/text()').get()
    32         content_list.append([ranking,name,province,total_score,index])
    33     # print( content_list)
    34 
    35 """
    36 将获取到的信息打印出来
    37 使用format方法按照一定的排序顺序来打印
    38 """
    39 def output_content(content_list,number):
    40     output ="{0:^10}	{1:{4}^10}	{2:^10}	{3:^10}" #{1:{4}^10}表示采用的填充符为format后面的第5个
    41     print(output.format('排行','学校名称','总分','指标得分',chr(12288)))    #chr(12288)中文空格填充符
    42     for i in range(number):
    43         u = content_list[i]
    44         # print(u)
    45         print(output.format(u[0],u[1],u[2],u[3],chr(12288)))
    46 
    47 if __name__ == '__main__':
    48     content_list = []
    49     hd = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36'
    50             }
    51     url = 'http://www.zuihaodaxue.com/zuihaodaxuepaiming2019.html'
    52     html = get_page_url(hd,url)
    53     # print(html)
    54     parsing_website(html,content_list)
    55     # print(content)
    56     output_content(content_list,100)
  • 相关阅读:
    隐私保护政策
    童真儿童简笔画
    方块十字消
    iOS 判断一断代码的执行时间(从网上看的,自己实现一下)
    iOS BLOCK回调:(妖妖随笔)
    typedef struct
    #define和预处理指令
    UIActivityIndicatorView
    Expected a type 的错误
    iOS 本地化字符串—(妖妖随笔)
  • 原文地址:https://www.cnblogs.com/zihkj/p/12273471.html
Copyright © 2011-2022 走看看