【发布时间】:2017-03-08 16:19:05
【问题描述】:
我有一个要运行的 URL 列表,使用 BeautifulSoup 清理并保存到 .txt 文件。
这是我现在的代码,列表中只有几个项目,还有更多来自 txt 文件的项目,但现在这让它变得简单。
当循环工作时,它将两个 URL 的输出传递给 URL.txt 文件。我希望列表中的每个实例都输出到其唯一的 .txt 文件中。
import urllib
from bs4 import BeautifulSoup
x = ["https://www.sec.gov/Archives/edgar/data/1000298/0001047469-13-002555.txt",
"https://www.sec.gov/Archives/edgar/data/1001082/0001104659-13-011967.txt"]
for url in x:
#I want to open the URL listed in my list
fp = urllib.request.urlopen(url)
test = fp.read()
soup = BeautifulSoup(test,"lxml")
output=soup.get_text()
#and then save the get_text() results to a unique file.
file=open("url.txt","w",encoding='utf-8')
file.write(output)
file.close()
感谢您观看。最好的,乔治
【问题讨论】:
-
你能做到
for i,url in enumerate(x):并使用i来构建url 文件名吗? -
我同意!下面的解释很好。