将函数中的项目写入单独的文本文件?
Writing items from function to separate text files?
我正在 运行 一些网络抓取,现在有一个 911 link 的列表保存在下面(我包括 5 个来演示它们是如何存储的):
every_link = ['http://www.millercenter.org/president/obama/speeches/speech-4427', 'http://www.millercenter.org/president/obama/speeches/speech-4425', 'http://www.millercenter.org/president/obama/speeches/speech-4424', 'http://www.millercenter.org/president/obama/speeches/speech-4423', 'http://www.millercenter.org/president/obama/speeches/speech-4453']
这些 URL link 随着时间的推移指向总统演讲。我想将每个单独的演讲(因此,911 独特的演讲)存储在不同的文本文件中,或者能够按总统分组。我正在尝试将以下功能传递给这些 links:
def processURL(l):
open_url = urllib2.urlopen(l).read()
item_soup = BeautifulSoup(open_url)
item_div = item_soup.find('div',{'id': 'transcript'},{'class': 'displaytext'})
item_str = item_div.text.lower()
item_str_processed = punctuation.sub('',item_str)
item_str_processed_final = item_str_processed.replace('—',' ')
for l in every_link:
processURL(l)
所以,我想将所有已处理的演讲中的单词保存到 唯一的 文本文件中。这可能类似于以下内容,其中 obama_44xx
代表单个文本文件:
obama_4427 = "blah blah blah"
obama_4425 = "blah blah blah"
obama_4424 = "blah blah blah"
...
我正在尝试以下操作:
for l in every_link:
processURL(l)
obama.write(processURL(l))
但这不起作用...
还有其他方法吗?
好的,您有几个问题。首先,您的 processURL
函数实际上并没有 return 任何东西,因此当您尝试编写函数的 return 值时,它将是 None
。也许尝试这样的事情:
def processURL(link):
open_url = urllib2.urlopen(link).read()
item_soup = BeautifulSoup(open_url)
item_div = item_soup.find('div',{'id': 'transcript'},{'class': 'displaytext'})
item_str = item_div.text.lower()
item_str_processed = punctuation.sub('',item_str)
item_str_processed_final = item_str_processed.replace('—',' ')
splitlink = link.split("/")
president = splitlink[4]
speech_num = splitlink[-1].split("-")[1]
filename = "{0}_{1}".format(president, speech_num)
return filename, item_str_processed_final # returning a tuple
for link in every_link:
filename, content = processURL(link) # yay tuple unpacking
with open(filename, 'w') as f:
f.write(content)
这会将每个文件写入一个类似于 president_number
的文件名。因此,例如,它会将 ID 号为 4427 的奥巴马演讲写入名为 obama_4427
的文件中。让我知道这是否有效!
您必须调用 processURL 函数并将其 return 您想要写入的文本。之后,您只需在循环中添加写入磁盘的代码即可。像这样:
def processURL(l):
open_url = urllib2.urlopen(l).read()
item_soup = BeautifulSoup(open_url)
item_div = item_soup.find('div',{'id': 'transcript'},{'class': 'displaytext'})
item_str = item_div.text.lower()
#item_str_processed = punctuation.sub('',item_str)
#item_str_processed_final = item_str_processed.replace('—',' ')
return item_str
for l in every_link:
speech_text = processURL(l).encode('utf-8').decode('ascii', 'ignore')
speech_num = l.split("-")[1]
with open("obama_"+speech_num+".txt", 'w') as f:
f.write(speech_text)
而.encode('utf-8').decode('ascii', 'ignore')
纯粹是为了处理文本中的非ascii字符。理想情况下,您会以不同的方式处理它们,但这取决于您的需要(参见 Python: Convert Unicode to ASCII without errors)。
顺便说一句,您列表中的第二个 link 是 404。您应该确保您的脚本可以处理它。
我正在 运行 一些网络抓取,现在有一个 911 link 的列表保存在下面(我包括 5 个来演示它们是如何存储的):
every_link = ['http://www.millercenter.org/president/obama/speeches/speech-4427', 'http://www.millercenter.org/president/obama/speeches/speech-4425', 'http://www.millercenter.org/president/obama/speeches/speech-4424', 'http://www.millercenter.org/president/obama/speeches/speech-4423', 'http://www.millercenter.org/president/obama/speeches/speech-4453']
这些 URL link 随着时间的推移指向总统演讲。我想将每个单独的演讲(因此,911 独特的演讲)存储在不同的文本文件中,或者能够按总统分组。我正在尝试将以下功能传递给这些 links:
def processURL(l):
open_url = urllib2.urlopen(l).read()
item_soup = BeautifulSoup(open_url)
item_div = item_soup.find('div',{'id': 'transcript'},{'class': 'displaytext'})
item_str = item_div.text.lower()
item_str_processed = punctuation.sub('',item_str)
item_str_processed_final = item_str_processed.replace('—',' ')
for l in every_link:
processURL(l)
所以,我想将所有已处理的演讲中的单词保存到 唯一的 文本文件中。这可能类似于以下内容,其中 obama_44xx
代表单个文本文件:
obama_4427 = "blah blah blah"
obama_4425 = "blah blah blah"
obama_4424 = "blah blah blah"
...
我正在尝试以下操作:
for l in every_link:
processURL(l)
obama.write(processURL(l))
但这不起作用... 还有其他方法吗?
好的,您有几个问题。首先,您的 processURL
函数实际上并没有 return 任何东西,因此当您尝试编写函数的 return 值时,它将是 None
。也许尝试这样的事情:
def processURL(link):
open_url = urllib2.urlopen(link).read()
item_soup = BeautifulSoup(open_url)
item_div = item_soup.find('div',{'id': 'transcript'},{'class': 'displaytext'})
item_str = item_div.text.lower()
item_str_processed = punctuation.sub('',item_str)
item_str_processed_final = item_str_processed.replace('—',' ')
splitlink = link.split("/")
president = splitlink[4]
speech_num = splitlink[-1].split("-")[1]
filename = "{0}_{1}".format(president, speech_num)
return filename, item_str_processed_final # returning a tuple
for link in every_link:
filename, content = processURL(link) # yay tuple unpacking
with open(filename, 'w') as f:
f.write(content)
这会将每个文件写入一个类似于 president_number
的文件名。因此,例如,它会将 ID 号为 4427 的奥巴马演讲写入名为 obama_4427
的文件中。让我知道这是否有效!
您必须调用 processURL 函数并将其 return 您想要写入的文本。之后,您只需在循环中添加写入磁盘的代码即可。像这样:
def processURL(l):
open_url = urllib2.urlopen(l).read()
item_soup = BeautifulSoup(open_url)
item_div = item_soup.find('div',{'id': 'transcript'},{'class': 'displaytext'})
item_str = item_div.text.lower()
#item_str_processed = punctuation.sub('',item_str)
#item_str_processed_final = item_str_processed.replace('—',' ')
return item_str
for l in every_link:
speech_text = processURL(l).encode('utf-8').decode('ascii', 'ignore')
speech_num = l.split("-")[1]
with open("obama_"+speech_num+".txt", 'w') as f:
f.write(speech_text)
而.encode('utf-8').decode('ascii', 'ignore')
纯粹是为了处理文本中的非ascii字符。理想情况下,您会以不同的方式处理它们,但这取决于您的需要(参见 Python: Convert Unicode to ASCII without errors)。
顺便说一句,您列表中的第二个 link 是 404。您应该确保您的脚本可以处理它。