FileNotFoundError:[Errno 2]没有这样的文件或目录:“transcripts/louis.txt”

2024-04-26 18:15:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试删除12个脚本,并希望对它们进行pickle,但由于某些原因,它无法正常工作,并且出现错误[Errno 2]没有这样的文件或目录:“transcripts/louis.txt”。 谁能帮我一下吗。非常感谢

刮网、泡菜进口

    import requests

    from bs4 import BeautifulSoup

    import pickle

    import os

import errno

从ScrapsFromtheSoft.com中删除成绩单数据

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

作用域中转录本的URL

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]

#Pickle文件供以后使用

#创建一个新目录以保存文本文件

!!mkdir转录本

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**`

Tags: textinimporttxtcomhttpurlfor