nova-week3

时间:2019-03-20
本文章向大家介绍nova-week3,主要包括nova-week3使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1.链接的跟踪和爬取

1.1寻找任何一个站点主页,例如www.nju.edu.cn将当前页面中的所有链接提取出来,用一个 json 格式的文件存储

1.1.1/2 基础知识和思考题

json文件格式的具体语法:SoJson在线编辑,格式化,Json语法介绍

python中的json库用法:
python json-菜鸟教程
json模块详解

  1. json.dumps函数相当于文件读写中的write,作用:把python对象编码为JSON字符串
    dumps:无文件操作 dump:序列化+写入文件
  2. json loads函数相当于文件读写中的read,作用:将已编码的 JSON 字符串解码为 Python 对象
    loads:无文件操作 load: 读文件+反序列化

不带s的用于操作文件,带s的用于数据类型的转换:

def store(data):
    with open('data.json', 'w') as fw:
        # 将字典转化为字符串
        json_str = json.dumps(data)
        fw.write(json_str)
        # 上面两句等同于下面这句
        json.dump(data,fw)
        fw.close()

将json语句格式化的代码:

data={"a":1,"b":2,"c":3,"d":4,"e":5}
Json=json.dumps(data,sort_keys=True, indent=4, separators=(',', ': '))
print(Json)

用python.json读写一个json文件的函数封装:

import  json
def json_write(file):
    with open(file,'a') as f:
        data={"a":1,"b":2,"c":3,"d":4,"e":5}
        Json=json.dumps(data)
        f.write(Json)
        f.close()
    return
def json_read(file):
    with open(file,'r') as f:
        Json=json.loads(f.read())
        #没有找到通过loads()方法直接格式化内容的办法
        Json=json.dumps(Json,sort_keys=True, indent=4, separators=(',', ': '))
        #格式化json代码,为了好看
        print(Json)
        f.close()
if __name__=="__main__":
    json_write(r"C:\Users\lenovo\Desktop\test.json")
    json_read(r"C:\Users\lenovo\Desktop\test.json")

将含中文的可迭代对象写入json文件:

#把中文形式的可迭代对象写入json文件中
import json
with open(r"C:\Users\lenovo\Desktop\test.json",'a') as f:
        data={"一":1,"二":2,"三":3,"四":4,"五":5}
        Json=json.dumps(data,sort_keys=True, indent=4, separators=(',', ': '))
        f.write(Json)
        f.close()

结果为:

{
    "\u4e00": 1,
    "\u4e09": 3,
    "\u4e8c": 2,
    "\u4e94": 5,
    "\u56db": 4
}

原因: 由于# – coding: utf-8 – 的作用,文件内容以utf-8编码,json文件中的是utf-8编码后的结果\u4e00
json.dumps 序列化时对中文默认使用的ascii编码
字符串在Python内部的表示是unicode编码。
因此,在做编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符串解码(decode)成unicode,再从unicode编码(encode)成另一种编码。
decode的作用是将其他编码的字符串转换成unicode编码;decode(’utf-8’)表示将utf-8编码的字符串转换成unicode编码。
encode的作用是将unicode编码转换成其他编码的字符串;encode(‘gb2312’),表示将unicode编码的字符串转换成gb2312编码。

处理中文问题的解决办法:

Json=json.dumps(data,ensure_ascii=False 

1.1.3任务代码

import requests
import json
from lxml import etree
def get_html(url):
    "获取网站的html代码并将其用lxml初始化,并返回"
    Html=requests.get(url)
    html=etree.HTML(Html.text)
    return html
def get_link(html):
    "获取html中的链接地址,并写入文件"
    with open(r"C:\Users\lenovo\Desktop\test.json",'a')as f:
        link=html.xpath("//p/a/@href")
        Json=json.dumps(link,sort_keys=True, indent=4, separators=(',', ': '))
        f.write(Json)
        f.close()
if __name__=="__main__":
    html=get_html(r'http://jw.nju.edu.cn/')
    get_link(html)

运行结果:

[
    "http://jwas3.nju.edu.cn:8080/jiaowu/flex.jsp",
    "http://jw.nju.edu.cn/jwcoa/Root/Default.asp",
    "http://elite.nju.edu.cn/",
    "http://desktop.nju.edu.cn/cx/index.aspx",
    "http://jwas3.nju.edu.cn:81/",
    "http://ctl.nju.edu.cn/",
    "http://tres.nju.edu.cn/dltr/default.htm",
    "http://bbs.nju.edu.cn/vd902154/bbsdoc?board=M_Academic",
    "http://www.njzhifeng.com"
]

注意!!!

不要在json文件中写任何的注释,注释会被当做一个顶级项! json就是一个对象 它必须是由[…]或者{…}构成的