Function 将项目从函数写入单独的文本文件?

Function 将项目从函数写入单独的文本文件?,function,python-2.7,web-scraping,beautifulsoup,Function,Python 2.7,Web Scraping,Beautifulsoup,我正在运行一些网络抓取,现在有一个保存在下面的911链接列表(我包括5个链接以演示它们是如何存储的): 随着时间的推移,这些URL链接到总统演讲。我想把每一篇演讲(911独特的演讲)存储在不同的文本文件中,或者可以按总统分组。我试图将以下函数传递给这些链接: def processURL(l): open_url = urllib2.urlopen(l).read() item_soup = BeautifulSoup(open_url) item_div = item_

我正在运行一些网络抓取,现在有一个保存在下面的911链接列表(我包括5个链接以演示它们是如何存储的):

随着时间的推移,这些URL链接到总统演讲。我想把每一篇演讲(911独特的演讲)存储在不同的文本文件中,或者可以按总统分组。我试图将以下函数传递给这些链接:

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)
因此,我想将所有经过处理的演讲中的单词保存到唯一的文本文件中。这可能如下所示,
obama44xx
表示单个文本文件:

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
函数实际上没有返回任何内容,因此当您尝试写入函数的返回值时,它将是
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)
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)

这将把每个文件写入一个类似于
总统号
的文件名。例如,它将把id号为4427的奥巴马演讲写入一个名为
Obama_4427
的文件中。让我知道这是否有效

您必须调用processURL函数,让它返回您想要写入的文本。之后,您只需在循环中添加写入磁盘的代码。大概是这样的:

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)
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字符。理想情况下,您会以不同的方式处理它们,但这取决于您的需要(请参阅)


顺便说一句,你列表中的第二个链接是404。你应该确保你的脚本可以处理这个问题。

还没什么。我正试图找出如何最好地存储不同迭代的
processURL
。因此,也许对于奥巴马的演讲,我可以将他的一篇演讲保存到一个文件(例如,
Obama_4427
)和另一个文件(例如,
Obama_4428
),等等。所有链接的格式都相同吗?i、 e.
http://www.millercenter.org/president//speeches/speech-
?是的。演讲数字似乎更随机(即,他们用来存储奥巴马演讲的数字可能不同于他们用来存储克林顿演讲的数字)。这是一个优雅的解决方案-谢谢!它将刮取的文本写入文本文件,并将它们存储在我的U:/drive中。这正是我想要的。。。再次感谢。当我最初发布链接时,有大约1600个我以前没有注意到的重复链接。所以,为了演示的目的,我刚刚提出了一些随机的44xx目录。而且,我在脚本前面处理了ASCII问题。谢谢你的帮助!