python-2.7 - 从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"
...
请留下您的任何建议。谢谢你 。
解决方案
有一些简单的方法我可以阅读 HTML,但它可以阅读页面的源代码:
import urllib2
for line in urllib2.urlopen("https://lite.cnn.com/en"):
file = open('testfile.txt','a')
file.write(line)
file.close()
推荐阅读
- java - 有没有办法在使用过程中指定传递给接口函数的类型?
- linux - 更改登录用户和端口后无法登录EC2
- list - 在 Flutter 中拆分字符串
- javascript - Chrome 代理:无效调用
- angular - 角度错误 - rxjs - 未捕获的类型错误:无法读取未定义的属性“调用”
- php - 如何在 WordPress 表单插件字段中返回标题而不是帖子 ID
- python-3.x - googleapiclient.errors.UnknownApiNameOrVersion:名称:youtubePartner 版本:v1
- c - C中第一个参数为空的选择性参数?表达式“var1 ?: var2”
- php - 使用html中的表线程将数据插入mysql
- r - 对每 n 行应用不同的函数