【发布时间】:2014-11-18 20:05:28
【问题描述】:
你好 stackoverflow 社区!
我仍在学习 Python 编码的细节,所以请原谅我即将发布的代码。
我目前正在尝试编写一个脚本,该脚本将使用 BS4 从http://kat.ph 抓取最新的媒体种子列表并将其保存到文件中。但是,我无法将 BS4 的输出打印到此文件。当您打开文本文件时,它是空白的,但是当您在终端中运行脚本时,它工作得很好。最终,我想让 python 在电子邮件中发送 bs4 输出(这是我最初遇到此问题并决定查看是否可以写入 .txt 文件的地方)。
目前我没有在家用电脑上制作的脚本,但我重新创建了另一个我做了几乎相同的事情的脚本。
非常感谢任何帮助/建议!
from bs4 import BeautifulSoup
import requests
import time
#The goal of this script was to scrape the names of the latest media torrents and write them to a text file.
#When I run the script on my computer, I can see the prompt give me the list of torrents just fine.
#When I try to write to a file or send an email, it doesn't print anything.
req = requests.get('http://kat.ph')
site = req.text
soup = BeautifulSoup(site) #Tried making this 'soup = str(BeautifulSoup(site)) to no avail.
def writingFunction():
#I imported time module because I had my script display the time and date here.
counter = 1
for i in soup.find_all('div', {'class': 'markeredBlock torType filmType'}):
print str(counter) + '.' + ' ' + i.text
counter = counter + 1
textFile = open('C:/python27/file.txt', 'a')
textFile.write(writingFunction()) #I've tried making this a str and I've also tried assigning the function to a variable
textFile.close()
【问题讨论】:
标签: python python-2.7 web-scraping beautifulsoup