0

I am trying to save output data after I am running successful script in python using Selenium. But, I am not able to save result at end of my run/ script. My code is running fine, only problem is I am not able to save out to a file which can be .json, csv or text. I need serious help on this one.

    from selenium import webdriver
    from bs4 import BeautifulSoup as bs
    import csv
    import requests


# saving data in bangkok_vendor.text
    def copy_info():
        with open('bangkok_vendor.text','a') as wt:
            for x in script3:
                wt.write(x)
                wt.close()
                return

    contents =[]

    filename = 'link_business_filter.csv'

    with open(filename,'rt') as f:
        data = csv.reader(f)
        for row in data:
            links = row[0]
            contents.append(links)

    for link in contents:
        url_html = requests.get(link)
        print(link)
        browser = webdriver.Chrome('chromedriver')
        open = browser.get(link)
        source = browser.page_source
        data = bs(source,"html.parser")
        body = data.find('body')
        script = body
        x_path = '//*[@id="react-root"]/section/main/div'
        script2 = browser.find_element_by_xpath(x_path)
        script3 = script2.text
        #script2.send_keys(keys.COMMAND + 't')
        browser.close()
        print(script3)
        copy_info()

1 Answers1

0

Did you try using csv.writer for csv files? Please check out the following link. hope it helps.

Save results to csv file with Python

Krunal Patel
  • 257
  • 3
  • 10