2014-03-04 72 views
1

我想把刮網址保存到文本文件,但是我在文件中找到的結果與打印的結果不同。我只能找到文件中的最後一組。Python寫網址到BeautifulSoup的文件

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() 

回答

2

當您在'w'(寫入)模式下打開文件時,每次都會覆蓋該文件。以追加模式打開文件:

file = open("file.txt", "a")