Python 学习笔记  105

作者: 夜羽萧轩 | 来源:发表于2020-01-22 23:32 被阅读0次

使用python自动爬取并保存武汉病毒的相关信息


哎,目前武汉新型肺炎病毒肆虐,想实时关注,但是变化太快,正好在学习python,就写了个程序实时爬取个动态看一看,以便后续的数据分析咯~~
同时,也希望武汉加油,湖北加油,全国加油,一起度过这场难关吧~~~
以上,话不多说,开始吧~~~


数据来源 丁香园的数据 →
https://3g.dxy.cn/newh5/view/pneumonia?from=singlemessage


代码重构记录

  1. 增加try except语句
    参考 → https://www.cnblogs.com/hjw1/p/8277825.html
    抓取失败 显示failed

    source code
    import requests
    url="http://www.cnblogs.com/hjw1"
    try:
    r=requests.get(url)
    r.raise_for_status()
    print r.encoding
    print r.text
    except:
    print "failed"

    code应该建立在python 2.x 上,需进一步探索

  2. 解码模块不可用
    参考 → https://blog.csdn.net/qq_42379006/article/details/80619412
    问题解决 代码重构

    source code

     # -*- coding:utf-8 -*-
     import requests
     import chardet
    
     url = 'https://search.51job.com'    # textURL  未复制完全
     r = requests.get(url)
     code = chardet.detect(r.content)['encoding']
     print(code)   #GB2312
     r.encoding = code
     print(r.text)
     #方法二:
     print(r.content.decode(code))
    

    成功解决编码问题

     print(code)   
     >>> utf-8
     res.text
     可以成功爬出源码并解码
    
  3. Xpath模块解析

    导入Xpath etree模块
    将模块转化为etree对象
    保存 新增cell调用

  1. list&Str互转 Xpath对象解析后为list类型,需要转化为文本

    第二次查询 ※※

    链接 → https://www.cnblogs.com/sench/p/8718008.html

    test_str = "".join(test_list)

  2. 病例数包含html格式化的文本,有颜色 span标签
    使用string() 函数解析
    链接 → https://blog.csdn.net/xufwind/article/details/88398863

  1. 程序定时执行
    待探索

效果图

以上吧,代码看心情贴出来~

相关文章

网友评论

    本文标题:Python 学习笔记  105

    本文链接:https://www.haomeiwen.com/subject/parbzctx.html