python出现RuntimeError错误问题及解决

 更新时间:2022年05月23日 10:29:41   作者:舔狗一无所有  
这篇文章主要介绍了python出现RuntimeError错误问题及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

下面是出现的错误解释

RuntimeError: 
        An attempt has been made to start a new process before the
        current process has finished its bootstrapping phase.
 
        This probably means that you are not using fork to start your
        child processes and you have forgotten to use the proper idiom
        in the main module:
 
            if __name__ == '__main__':
                freeze_support()
                ...
 
        The "freeze_support()" line can be omitted if the program
        is not going to be frozen to produce an executable.

下面是出现错误代码的原代码

import multiprocessing as mp
import time
from urllib.request import urlopen,urljoin
from bs4 import BeautifulSoup
import re
 
base_url = "https://morvanzhou.github.io/"
 
#crawl爬取网页
def crawl(url):
    response = urlopen(url)
    time.sleep(0.1)
    return response.read().decode()
 
#parse解析网页
def parse(html):
    soup = BeautifulSoup(html,'html.parser')
    urls = soup.find_all('a',{"href":re.compile('^/.+?/$')})
    title = soup.find('h1').get_text().strip()
    page_urls = set([urljoin(base_url,url['href'])for url in urls])
    url = soup.find('meta',{'property':"og:url"})['content']
    return title,page_urls,url
 
unseen = set([base_url])
seen = set()
restricted_crawl = True
 
pool = mp.Pool(4)
count, t1 = 1, time.time()
while len(unseen) != 0:                 # still get some url to visit
    if restricted_crawl and len(seen) > 20:
            break
    print('\nDistributed Crawling...')
    crawl_jobs = [pool.apply_async(crawl, args=(url,)) for url in unseen]
    htmls = [j.get() for j in crawl_jobs]      # request connection
 
    print('\nDistributed Parsing...')
    parse_jobs = [pool.apply_async(parse, args=(html,)) for html in htmls]
    results = [j.get() for j in parse_jobs]    # parse html
 
    print('\nAnalysing...')
    seen.update(unseen)         # seen the crawled
    unseen.clear()              # nothing unseen
 
    for title, page_urls, url in results:
        print(count, title, url)
        count += 1
        unseen.update(page_urls - seen)     # get new url to crawl
print('Total time: %.1f s' % (time.time()-t1))    # 16 s !!!

这是修改后的正确代码

import multiprocessing as mp
import time
from urllib.request import urlopen,urljoin
from bs4 import BeautifulSoup
import re
 
base_url = "https://morvanzhou.github.io/"
 
#crawl爬取网页
def crawl(url):
    response = urlopen(url)
    time.sleep(0.1)
    return response.read().decode()
 
#parse解析网页
def parse(html):
    soup = BeautifulSoup(html,'html.parser')
    urls = soup.find_all('a',{"href":re.compile('^/.+?/$')})
    title = soup.find('h1').get_text().strip()
    page_urls = set([urljoin(base_url,url['href'])for url in urls])
    url = soup.find('meta',{'property':"og:url"})['content']
    return title,page_urls,url
 
def main():
    unseen = set([base_url])
    seen = set()
    restricted_crawl = True
 
    pool = mp.Pool(4)
    count, t1 = 1, time.time()
    while len(unseen) != 0:                 # still get some url to visit
        if restricted_crawl and len(seen) > 20:
                break
        print('\nDistributed Crawling...')
        crawl_jobs = [pool.apply_async(crawl, args=(url,)) for url in unseen]
        htmls = [j.get() for j in crawl_jobs]      # request connection
 
        print('\nDistributed Parsing...')
        parse_jobs = [pool.apply_async(parse, args=(html,)) for html in htmls]
        results = [j.get() for j in parse_jobs]    # parse html
 
        print('\nAnalysing...')
        seen.update(unseen)         # seen the crawled
        unseen.clear()              # nothing unseen
 
        for title, page_urls, url in results:
            print(count, title, url)
            count += 1
            unseen.update(page_urls - seen)     # get new url to crawl
    print('Total time: %.1f s' % (time.time()-t1))    # 16 s !!!
 
 
if __name__ == '__main__':
    main()

综上可知,就是把你的运行代码整合成一个函数,然后加入

if __name__ == '__main__':
    main()

这行代码即可解决这个问题。

python报错:RuntimeError

python报错:RuntimeError:fails to pass a sanity check due to a bug in the windows runtime这种类型的错误

这种错误原因

1.当前的python与numpy版本之间有什么问题,比如我自己用的python3.9与numpy1.19.4会导致这种报错。

2.numpy1.19.4与当前很多python版本都有问题。

解决办法

在File->Settings->Project:pycharmProjects->Project Interpreter下将numpy版本降下来就好了。

1.打开interpreter,如下图:

第一步

2.双击numpy修改其版本:

在这里插入图片描述

3.勾选才能修改版本,将需要的低版本导入即可:

第三步

弄完了之后,重新运行就好。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • 提升python处理速度原理及方法实例

    提升python处理速度原理及方法实例

    这篇文章主要介绍了提升python处理速度原理及方法实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-12-12
  • 基于python实现模拟数据结构模型

    基于python实现模拟数据结构模型

    这篇文章主要介绍了基于python实现模拟数据结构模型,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-06-06
  • Pyhton中单行和多行注释的使用方法及规范

    Pyhton中单行和多行注释的使用方法及规范

    大家都知道python中的注释有多种,有单行注释,多行注释,批量注释,中文注释也是常用的。python注释也有自己的规范,这篇文章文章中会给大家详细介绍Pyhton中单行和多行注释的使用方法及规范,有需要朋友们可以参考借鉴。
    2016-10-10
  • Python实现线性拟合及绘图的示例代码

    Python实现线性拟合及绘图的示例代码

    在数据处理和绘图中,我们通常会遇到直线或曲线的拟合问题,本文主要介绍了Python实现线性拟合及绘图的示例代码,具有一定的参考价值,感兴趣的可以了解一下
    2024-04-04
  • Python把csv文件转换为excel文件

    Python把csv文件转换为excel文件

    本文主要介绍了Python把csv文件转换为excel文件,可以使用xlrd,xlrwt,openpyxl,xlwings,pandas 等库操作 Excel,具有一定的参考价值,感兴趣的可以了解一下
    2024-04-04
  • python 基于PYMYSQL使用MYSQL数据库

    python 基于PYMYSQL使用MYSQL数据库

    这篇文章主要介绍了python 基于PYMYSQL使用MYSQL数据库的方法,帮助大家更好的理解和使用python,感兴趣的朋友可以了解下
    2020-12-12
  • 详解Python利用APScheduler框架实现定时任务

    详解Python利用APScheduler框架实现定时任务

    在做一些python工具的时候,常常会碰到定时器问题,总觉着使用threading.timer或者schedule模块非常不优雅。所以本文将利用APScheduler框架实现定时任务,需要的可以参考一下
    2022-03-03
  • 快速进修Python指南之控制if-else循环技巧

    快速进修Python指南之控制if-else循环技巧

    这篇文章主要为大家介绍了Java开发者的Python快速进修指南之控制之if-else和循环技巧示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-12-12
  • Python json 错误xx is not JSON serializable解决办法

    Python json 错误xx is not JSON serializable解决办法

    这篇文章主要介绍了Python json 错误xx is not JSON serializable解决办法的相关资料,需要的朋友可以参考下
    2017-03-03
  • NumPy 数组属性的具体使用

    NumPy 数组属性的具体使用

    本文主要介绍了NumPy 数组属性的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-08-08

最新评论