我正在尝试将已删除的网址保存到文本文件中,但我在文件中找到的结果与打印的结果不同。我只找到文件中的最后一组。
urls = ["http://google.com/page=","http://yahoo.com"]
for url in urls:
for number in range(1,10):
conn = urllib2.urlopen(url+str(number))
html = conn.read()
soup = BeautifulSoup(html)
links = soup.find_all('a')
file= open("file.txt","w")
for tag in links:
link = tag.get('href')
print>>file, link
print link
file.close()
答案 0 :(得分:2)
当您以'w'
(写入)模式打开文件时,文件每次都会被覆盖。以追加模式打开文件:
file = open("file.txt", "a")