Python Экспорт списка селен в текстовый файл - PullRequest
1 голос
/ 17 апреля 2020

Код ниже был предназначен для получения названий видео курсов. Ну, он находит его, но я не могу записать вывод в текстовый файл.

from selenium import webdriver
f=("output.txt", "a")

browsing_1 = webdriver.Firefox(executable_path="C:\\Tester\\geckodriver.exe")
browsing_1.get("https://channel9.msdn.com/Niners/JeffKoch/Posts?page=170")

testing_texts = browsing_1.find_elements_by_xpath("//div[@class='seriesTitle']")
for namess in testing_texts:
    print(namess.text)

Когда я использую этот f.write (names), он выдает следующее:

Traceback (most recent call last):
  File "C:\Tester\trial83.py", line 11, in <module>
    f.write(names)
AttributeError: 'tuple' object has no attribute 'write'

Могу ли я в любом случае добавить файл и получить приведенный ниже вывод, пожалуйста?

Link:"https://channel9.msdn.com/Niners/JeffKoch/Posts?page=170"
System Center 2012 R2 Infrastructure Provisioning and Management
Core Solutions of Exchange Server 2013
Core Solutions of Exchange Server 2013
Core Solutions of Exchange Server 2013
Core Solutions of Exchange Server 2013
Core Solutions of Exchange Server 2013
Core Solutions of Exchange Server 2013
Core Solutions of Exchange Server 2013
Windows Server 2012 R2 Storage
Windows Server 2012 R2 Storage
Windows Server 2012 R2 Storage
Windows Server 2012 R2 Storage

1 Ответ

2 голосов
/ 18 апреля 2020

Я использовал list для хранения всех значений очищенных данных, а затем преобразовал этот список в файл .txt.

from selenium import webdriver

browsing_1 = webdriver.Firefox(executable_path="C:\\Users\\intel\\Downloads\\Setups\\geckodriver.exe")
browsing_1.get("https://channel9.msdn.com/Niners/JeffKoch/Posts?page=170")

testing_texts = browsing_1.find_elements_by_xpath("//div[@class='seriesTitle']")

# created an empty list
lis = []
for namess in testing_texts:
    print(namess.text)
    lis.append(namess.text)   # appending that empty list to store further values

print(lis)

#remember to add a full correct path to your file just like i did.   
with open(r'C:\Users\intel\Desktop\output.txt', 'a') as f:   
    for item in lis:
        f.write("%s\n" % item)

Он работал просто отлично. Попробуй это. Надеюсь, это желаемый результат, который вы хотите.

...