首页 > 解决方案 > 从html文本链接到python 2中的txt文件

问题描述

我只需要在 python 2 中编写脚本的帮助,该脚本将从该页面获取标题:https ://lite.cnn.com/en ,并将其逐行保存在文本文件中,如下所示:

"Trump, Macron gloss over differences in France after rough start 
Trump spars with Macron as Air Force One lands in France
Opinion: Which President Trump will show up in Paris?
Two leaders holding bilateral talks"
...

请留下您的任何建议。谢谢你 。

标签: python-2.7

解决方案


有一些简单的方法我可以阅读 HTML,但它可以阅读页面的源代码:

import urllib2
for line in urllib2.urlopen("https://lite.cnn.com/en"):
    file = open('testfile.txt','a')
    file.write(line)
    file.close()

推荐阅读