Search code examples
python-3.xseleniumcsvexport-to-csv

Python CSV output to columns from two lists


I'm grabbing search results from bing. Everything is working except the output to the csv file. I've tried pandas also but can't seem to get the output right. I need the "url" in column A and "name" in column B next to the corresponding link.

example search link

def scrape():
    urls = WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.CLASS_NAME, "b_algo")))
    url = [div.find_element_by_tag_name('a').get_attribute('href') for div in urls]
    names = WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.CLASS_NAME, "b_algo")))
    name = [div.find_element_by_tag_name('h2 > a').get_attribute('innerHTML').split('-')[0].strip() for div in names]

   x1 = [url]
   x2 = [name]

   pp.pprint([url,name])

   with open(bing_parameters.file_name, 'a', newline='\n', encoding='utf-8') as f:
       wr = csv.writer(f)
       for items in x1:
           wr.writerow([x1,x2])
scrape()

Solution

  • Try this out. To put url to the first column and name to second column and then write to csv.

    import pandas as pd
    df = pd.DataFrame(url)
    df.columns =['A']
    df['B']=name
    print(df)
    df.to_csv(bing_parameters.file_name, index=False)