2014-03-04 1 views
1

긁힌 된 URL을 텍스트 파일에 저장하려고하지만 파일에서 찾은 결과가 인쇄 된 결과와 다릅니다. 파일에서 마지막 세트 만 찾습니다. 당신이 'w' (쓰기) 모드에서 파일을 연 것처럼Python이 BeautifulSoup를 사용하여 파일에 URL을 작성하는 중

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

, 파일마다 덮어 쓰기됩니다. 추가 모드에서 파일을 엽니 다.

file = open("file.txt", "a") 
관련 문제