Python Selenium List导出到文本文件



下面的代码旨在获得视频课程的标题。它找到了,但我无法将输出写入文本文件。

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(名称(时,它会给出以下内容:

Traceback (most recent call last):
File "C:Testertrial83.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

我使用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:UsersintelDesktopoutput.txt', 'a') as f:   
for item in lis:
f.write("%sn" % item)

它运行得非常好。试试这个。希望这是你想要的输出。

相关内容

  • 没有找到相关文章

最新更新