Python 如何将web抓取的数据从selenium保存到.txt文件

Python 如何将web抓取的数据从selenium保存到.txt文件,python,selenium,Python,Selenium,我对Python非常陌生,刚刚完成了“用Python自动化无聊的东西”课程。我有一个脚本,可以很好地访问站点,获取我需要的所有必要数据,并将其打印到我的控制台。但是,我在如何实际保存/导出数据到文件方面遇到了一个问题。现在我希望能够将其导出为.txt或.csv文件。非常感谢您的帮助,因为我在网上找不到直接的答案。我只需要最后一步来完成我的项目,谢谢 from selenium import webdriver from selenium.webdriver.support.ui import W

我对Python非常陌生,刚刚完成了“用Python自动化无聊的东西”课程。我有一个脚本,可以很好地访问站点,获取我需要的所有必要数据,并将其打印到我的控制台。但是,我在如何实际保存/导出数据到文件方面遇到了一个问题。现在我希望能够将其导出为.txt或.csv文件。非常感谢您的帮助,因为我在网上找不到直接的答案。我只需要最后一步来完成我的项目,谢谢

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
browser = webdriver.Chrome()

def getTen():

# Opens the browser and navigates to the page
browser.get('http://taxsales.lgbs.com/map?lat=29.437693458470175&lon=-98.4618145&zoom=9&offset=0&ordering=sale_date,street_name,address_full,uid&sale_type=SALE,RESALE,STRUCK%20OFF,FUTURE%20SALE&county=BEXAR%20COUNTY&state=TX&in_bbox=-99.516502,28.71637426279382,-97.407127,30.153924134433552')

# Waits until the page is loaded then clicks the accept button on the popup window
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[2]/div/div/div[2]/button[1]"))).click()



    # Loops through 10 times, changing the listing number to correspond with 1
for i in range(1,11):

    clickable = "/html/body/ui-view/div[2]/main/aside/div[2]/property-listing[" + str(i) + "]/article/div/a"

        # Waits until the page is loaded then clicks the view more details button on the first result
    WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, clickable))).click()

# Waits until the page is loaded, then pulls all the info from the top section of the page
    info = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, "/html/body/ui-view/div/main/div/div/div[2]/property-detail-info/dl[1]")))

    # prints info to the console
    print(info.text);

    # goes back a page and repeats the process for the next listing
    browser.back()

getTen()

如果您试图保存
info.text
,只需打开一个本地文件并写入即可。例如:

with open('output.txt', 'w') as f:
    f.write(info.text)

有关读取和写入文件的更多信息,请参见此处:

您只需在控制台中使用输出/获取文本,并使用下面的代码在.txt中获取文本即可 output.txt将是您要保存的文件名,element.text将是您要保存在.txt中的element或text

将open('output.txt','w')作为f:


非常感谢你。这正是我要找的!这是一个非常基本的问题,被包装在一个更复杂的背景中
info.text
是一个字符串(或者至少可以使用
str(info.text)
将该字符串转换为字符串),并且您希望将该字符串保存到文件中。你的问题的答案与你的问题的答案相同。
f.write(elements.text)