Python使用BeautifulSoup将URL写入文件
我正试图将抓取的URL保存到文本文件中,但在文件中找到的结果与打印的结果不同。我只在文件中找到最后一组Python使用BeautifulSoup将URL写入文件,python,beautifulsoup,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)
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()
当您以
'w'
(写入)模式打开文件时,每次都会覆盖该文件。以附加模式打开文件:
file = open("file.txt", "a")