0
出力ファイルにリンクの一覧が表示されますが、すべてのリンクを絶対リンクとして表示する必要があります。絶対的なものと相対的なものがあります。どのように私は親戚にベースURLを追加して、csv出力に絶対的なリンクしか得られないようにしますか?私はすべてのリンクではなく、すべてを取り戻すBeatifulsoup Python 3と絶対リンクを作成するためのベースURLを追加できません
はurljoin
との代わりにhttp://page.com/subpage
from bs4 import BeautifulSoup
import requests
import csv
j = requests.get("http://cnn.com").content
soup = BeautifulSoup(j, "lxml")
#only return links to subpages e.g. a tag that contains href
data = []
for url in soup.find_all('a', href=True):
print(url['href'])
data.append(url['href'])
print(data)
with open("file.csv",'w') as csvfile:
write = csv.writer(csvfile, delimiter = ' ')
write.writerows(data)
content = open('file.csv', 'r').readlines()
content_set = set(content)
cleandata = open('file.csv', 'w')
for line in content_set:
cleandata.write(line)