I'm trying to export rankings and URLs based on a specific keyword from Google and export it to a csv. Everything works fine except for the way my CSV file is composed.
My problem is that when I export my array to CSV, everything is on one row. I want it to be on 2 different columns and multiple rows.
I have tried adding '\n' inside the rank variable and it breaks the line in the terminal when I print the results, but inside the csv it appends the \n as a new value.
I tried adding newline='\n'to open() and it did break in the terminal but not in the csv export.
d=[]
for i in links:
counter = counter + 1
if sitename in str(i):
url = i.find_all('a', href=True)
position = "position: %d" % (counter)
rank = "url: %s" % (url[0]['href'])
d.append(position)
d.append(rank)
print(position, rank)
filename = "rank_export.csv"
f = open(filename,"w")
headers = "URL, Rank\n"
f.write(headers)
f.write(str(d))
f.close()
python df.to_csv(index=False)