我一直在尝试删除12个脚本,并希望对它们进行pickle,但由于某些原因,它无法正常工作,并且出现错误[Errno 2]没有这样的文件或目录:“transcripts/louis.txt”。 谁能帮我一下吗。非常感谢
刮网、泡菜进口
import requests
from bs4 import BeautifulSoup
import pickle
import os
import errno
def url_to_transcript(url):
'''Returns transcript data specifically from scrapsfromtheloft.com.'''
page = requests.get(url).text
soup = BeautifulSoup(page, "lxml")
text = [p.text for p in soup.find(class_="post-content").find_all('p')]
print(url)
return text
urls = ['http://scrapsfromtheloft.com/2017/05/06/louis-ck-oh-my-god-full-transcript/',
'http://scrapsfromtheloft.com/2017/04/11/dave-chappelle-age-spin-2017-full-transcript/',
'http://scrapsfromtheloft.com/2018/03/15/ricky-gervais-humanity-transcript/',
'http://scrapsfromtheloft.com/2017/08/07/bo-burnham-2013-full-transcript/',
'http://scrapsfromtheloft.com/2017/05/24/bill-burr-im-sorry-feel-way-2014-full-transcript/',
'http://scrapsfromtheloft.com/2017/04/21/jim-jefferies-bare-2014-full-transcript/',
'http://scrapsfromtheloft.com/2017/08/02/john-mulaney-comeback-kid-2015-full-transcript/',
'http://scrapsfromtheloft.com/2017/10/21/hasan-minhaj-homecoming-king-2017-full-transcript/',
'http://scrapsfromtheloft.com/2017/09/19/ali-wong-baby-cobra-2016-full-transcript/',
'http://scrapsfromtheloft.com/2017/08/03/anthony-jeselnik-thoughts-prayers-2015-full-transcript/',
'http://scrapsfromtheloft.com/2018/03/03/mike-birbiglia-my-girlfriends-boyfriend-2013-full-transcript/',
'http://scrapsfromtheloft.com/2017/08/19/joe-rogan-triggered-2016-full-transcript/']
# Comedian names
comedians = ['louis', 'dave', 'ricky', 'bo', 'bill', 'jim', 'john', 'hasan', 'ali', 'anthony', 'mike', 'joe']
transcripts = [url_to_transcript(u) for u in urls]
for i, c in enumerate(comedians):
with open("transcripts/" + c + ".txt", "wb") as file:
pickle.dump(transcripts[i], file)
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-6-f3ce7b5495e7> in <module>
5
6 for i, c in enumerate(comedians):
----> 7 with open("transcripts/" + c + ".txt", "wb") as file:
8 pickle.dump(transcripts[i], file)
FileNotFoundError: [Errno 2] No such file or directory: 'transcripts/louis.txt'`**enter code here**`
你的工作目录是什么?运行
os.getcwd()
以查找。您希望使用的文件夹结构需要在其中相关问题 更多 >
编程相关推荐