程序从网站获取标题列表。然后存储在一个变量(listOfjobs)中。我希望能够将该变量的内容(作业位置列表)附加到现有文本文件中,但它告诉我只接受字符串。我如何将纯文本的listOfJobs值的打印输出包含在文本文件中?谢谢!Python:将变量结果作为文本包装以将其追加到文本文件
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('https://jobs.theguardian.com/searchjobs/?LocationId=1500&keywords=personal+assistant&radialtown=London+(Central)%2c+London+(Greater)&countrycode=GB')
elem = browser.find_elements_by_class_name('lister__header')
for el in elem:
listOfJobs = print(el.text)
print(listOfJobs)
import os
helloFile = open('C:\\Users\\sk\\PycharmProjects\\test\\test_email.txt', 'w')
helloFile.write(websiteText)
helloFile.close()
这工作。非常感谢您发布完整的代码,帮助我学习。通过使用“with”,我看到你在代码中保存了行,但是我从来没有在基本编程课程中了解到这个命令。任何人都知道最好的资源来了解更多关于如何使用“with”命令,或者如何使用像@Padraic Cunningham这样的更少的行? – skeitel
@skeitel,不用担心,'with'会自动关闭你的文件,https://www.python.org/dev/peps/pep-0343/,如果你想从基础教程的ups这是一个很好的资源http://anandology.com/python-practice-book/getting-started.html –
感谢电脑。我发现这看起来更短,更清晰: http://effbot.org/zone/python-with-statement.htm – skeitel