读取html文档内容
# 读取html文件
def get_html(path, city):
# 读入
f = open(path, "r", encoding="utf-8")
# 获取内容
str1 = f.read()
# 替换内容
str2 = str1.replace('题花', city)
return str2
# 抓取html
html = get_html('index2.html', 郑州)
# html就是获取到index2.html中所有包含‘提花’的关键词替换为‘郑州’之后的内容。
新建文件夹
# 新建文件夹
try:
city = 'zhengzhou'
path = city
os.mkdir(path)
except:
pass
# try: 为判断zhengzhou文件夹名是否存在,不存在就新建文件夹,存在就忽略,继续往下执行!
保存内容
# 保存内容
f = open( city + 'index_2.html', 'a', encoding='utf-8')# f.seek(0)
# f.truncate()
f.write(html)
f.close()
# 如果已经存在 index_2.html并需要清空其内容后,再执行存入。需要写入清空指令:
# f.seek(0) # 0为从文档中字符第0位开始
# f.truncate() # 清空指令
python解析json
city = {'id':24, 'en': "henan", 'cn': "河南"}
# py解析city.cn
city['cn']