Here is my code, basically I wanted to output the variable "final" to excel and I wanted it to be printed in one column. My current writing code will only write the results to one row in excel..
import requests
from bs4 import BeautifulSoup
import urllib
import re
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import csv
r = requests.get("http://ift.tt/2ialoDq")
soup = BeautifulSoup(r.content)
g_data = soup.find_all("div", {"id":"scroller"})
for item in g_data:
regex = '.html">(.+?)</a>'
pattern = re.compile(regex)
htmlfile = urllib.urlopen("http://ift.tt/2ialoDq")
htmltext = htmlfile.read()
final = re.findall(pattern,htmltext)
with open('index4.csv','w') as csv_file:
writer = csv.writer(csv_file)
writer.writerow(['company'])#row?
writer.writerows([final])
Any possible fix for this? Thanks, I am just new to Python and just studying it with little programming knowledge.
Aucun commentaire:
Enregistrer un commentaire