![](/img/trans.png)
[英]How to export a list to web page text box using selenium webdriver in python?
[英]Python Selenium List Export to text file
下面的代碼旨在獲取視頻課程的標題。 好吧,它找到了,但我無法將 output 寫入文本文件。
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'
無論如何,我可以 append 文件並在其中包含以下 output 嗎?
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:\Users\intel\Desktop\output.txt', 'a') as f:
for item in lis:
f.write("%s\n" % item)
它工作得很好。 嘗試這個。 希望這是您想要的 output。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.