【发布时间】:2019-07-20 20:18:30
【问题描述】:
我正在尝试从 sololearn 网站备份我的代码。我当然可以复制/粘贴它,但因为我想为其他代码重复它,也为了学习目的,我想用 python 代码来做,并且尽可能只使用标准库。
我在这里介绍更基本的尝试。我也一直在努力处理 HTMLParser、html.entities、xml.etree,我尝试将响应解码为“utf-8”,以通过 html.unescape() 传递它。结果总是很脏。 这种脏:\u003c!DOCTYPE html\u003e\r\n\u003chtml\u003e\r\n\u003c!--\r\ 有时少,但从不清理
from urllib.request import urlopen
import re
url = "https://code.sololearn.com/************/#"
with urlopen(url) as response:
page = str(response.read())
code = re.search(r'window.code = "(.*)";.*window.cssCode',page).group(1)
print(code)
目标是备份我的文件,以干净的函数形式将它们写入文件,代码可以是 html+css+js、python、c 等...我还尝试使用正则表达式处理脏结果修改,但我认为这是不可能的,因为代码可能包含不应修改的故意元素,如“\r\n”。
【问题讨论】:
标签: python html json python-3.x urllib