我正在嘗試從網(wǎng)站上抓取 URL 并使用一組將它們發(fā)送到 .CSV 文件,以便刪除重復(fù)的 URL。我了解集合是什么以及如何創(chuàng)建集合,我只是不明白如何將網(wǎng)絡(luò)抓取的數(shù)據(jù)發(fā)送到集合。我假設(shè)它在 for 循環(huán)中,但我是 Python 新手并且不太確定。這是我的代碼的尾端:url_list=soup.find_all('a')with open('HTMLList.csv','w',newline="") as f: writer=csv.writer(f,delimiter=' ',lineterminator='\r') for link in url_list: url=str(link.get('href')) if url: if 'https://www.example.com' not in url: url = 'https://www.example.com' + url writer.writerow([url])f.close()我知道我需要?jiǎng)?chuàng)建一個(gè) set() 并將 URL 添加到該集合中,但我不確定如何并且我被告知它也將刪除任何重復(fù)項(xiàng),這會(huì)很棒。任何幫助將非常感激。謝謝!
如何將網(wǎng)頁(yè)抓取的數(shù)據(jù)添加到 Python 中的集合
尚方寶劍之說(shuō)
2021-10-05 16:22:54