문제

I am trying to save scraped urls to a text file but the results I find in the file are different from the printed ones. I only find the last set in the file.

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()
도움이 되었습니까?

해결책

As you have opened the file in 'w' (write) mode, the file gets overwritten every time. Open the file in append mode:

file = open("file.txt", "a")
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top