How to append a new string element in python? -
i web-scraping 2 table 2 different sites. want append new column (called wherefrom in header) web-scraping text, in code called "name".
my code here:
from bs4 import beautifulsoup selenium import webdriver import time import urllib2 import unicodecsv csv import os import sys import io import time import datetime import pandas pd bs4 import beautifulsoup import re import contextlib import selenium.webdriver.support.ui ui filename=r'output.csv' resultcsv=open(filename,"wb") output=csv.writer(resultcsv, delimiter=';',quotechar = '"', quoting=csv.quote_nonnumeric, encoding='latin-1') output.writerow(['time','flight','from','airlane','aircraft','status','wherefrom', 'actualdate']) def scrape(urls): browser = webdriver.firefox() url in urls: browser.get(url) html = browser.page_source soup=beautifulsoup(html,"html.parser") table = soup.find('table', { "class" : "table table-condensed table-hover data-table m-n-t-15" }) soup2=beautifulsoup(html,"html.parser") name = soup2.find('div' , attrs={'class' : 'row m-t-l m-l-l'}) datatable=[] record in table.find_all('tr', class_="hidden-xs hidden-sm ng-scope"): temp_data = [] data in record.find_all("td"): temp_data.append(data.text.encode('latin-1')) newlist = filter(none, temp_data) datatable.append(newlist) print name output.writerows(datatable) resultcsv.close() time.sleep(10) browser.close() urls = ["https://www.flightradar24.com/data/airports/bud/arrivals", "https://www.flightradar24.com/data/airports/fco/arrivals"] scrape(urls) resultcsv.close()
how can in loop, , how can correctly? because after writing these data csv, delimiter ; .
but after web-scraping tables there isn't ; in last text, think have insert ; in last text too?!
i talking this:
"1:15 pm";" kl1975";"amsterdam (ams)-";"klm";"b737 (ph-bgt) ";"landed 1:01 pm"
edited actual date (not working, format issue):
df = pd.dataframe(newlist) = time.strftime('%d-%m-%y') df['actualdate'] = #df.rows = header df.to_csv('output.csv', sep=';', encoding='latin-1', index=false)
i wrote in loop, see actual date (hours-minutes too, day)
it seems trivial i'm not sure understood question... if want add name
last element of each row in csv, have is, well, add last element of rows you're passing csv writer:
for record in table.find_all('tr', class_="hidden-xs hidden-sm ng-scope"): temp_data = [] data in record.find_all("td"): temp_data.append(data.text.encode('latin-1')) # here temp_data.append(name)
Comments
Post a Comment