欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python出現(xiàn)RuntimeError錯(cuò)誤問(wèn)題及解決

 更新時(shí)間:2022年05月23日 10:29:41   作者:舔狗一無(wú)所有  
這篇文章主要介紹了python出現(xiàn)RuntimeError錯(cuò)誤問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

下面是出現(xiàn)的錯(cuò)誤解釋

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.

下面是出現(xiàn)錯(cuò)誤代碼的原代碼

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爬取網(wǎng)頁(yè)
def crawl(url):
    response = urlopen(url)
    time.sleep(0.1)
    return response.read().decode()
 
#parse解析網(wǎng)頁(yè)
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爬取網(wǎng)頁(yè)
def crawl(url):
? ? response = urlopen(url)
? ? time.sleep(0.1)
? ? return response.read().decode()
?
#parse解析網(wǎng)頁(yè)
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()

綜上可知,就是把你的運(yùn)行代碼整合成一個(gè)函數(shù),然后加入

if __name__ == '__main__':
? ? main()

這行代碼即可解決這個(gè)問(wèn)題。

python報(bào)錯(cuò):RuntimeError

python報(bào)錯(cuò):RuntimeError:fails to pass a sanity check due to a bug in the windows runtime這種類型的錯(cuò)誤

這種錯(cuò)誤原因

1.當(dāng)前的python與numpy版本之間有什么問(wèn)題,比如我自己用的python3.9與numpy1.19.4會(huì)導(dǎo)致這種報(bào)錯(cuò)。

2.numpy1.19.4與當(dāng)前很多python版本都有問(wèn)題。

解決辦法

在File->Settings->Project:pycharmProjects->Project Interpreter下將numpy版本降下來(lái)就好了。

1.打開(kāi)interpreter,如下圖:

第一步

2.雙擊numpy修改其版本:

在這里插入圖片描述

3.勾選才能修改版本,將需要的低版本導(dǎo)入即可:

第三步

弄完了之后,重新運(yùn)行就好。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • pandas多級(jí)分組實(shí)現(xiàn)排序的方法

    pandas多級(jí)分組實(shí)現(xiàn)排序的方法

    下面小編就為大家分享一篇pandas多級(jí)分組實(shí)現(xiàn)排序的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • Python的Scrapy框架解析

    Python的Scrapy框架解析

    這篇文章主要為大家介紹了Python的Scrapy框架解析 ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2021-12-12
  • python基礎(chǔ)入門學(xué)習(xí)筆記(Python環(huán)境搭建)

    python基礎(chǔ)入門學(xué)習(xí)筆記(Python環(huán)境搭建)

    這篇文章主要介紹了python基礎(chǔ)入門學(xué)習(xí)筆記,這是開(kāi)啟學(xué)習(xí)python基礎(chǔ)知識(shí)的第一篇,夯實(shí)Python基礎(chǔ),才能走的更遠(yuǎn),感興趣的小伙伴們可以參考一下
    2016-01-01
  • 淺析Python 引號(hào)、注釋、字符串

    淺析Python 引號(hào)、注釋、字符串

    這篇文章主要介紹了Python 引號(hào)、注釋、字符串的相關(guān)知識(shí),文中給大家提到了python中一對(duì)單引號(hào),一對(duì)雙引號(hào),三個(gè)單雙引號(hào)的區(qū)別和用法,需要的朋友可以參考下
    2019-07-07
  • python實(shí)現(xiàn)JAVA源代碼從ANSI到UTF-8的批量轉(zhuǎn)換方法

    python實(shí)現(xiàn)JAVA源代碼從ANSI到UTF-8的批量轉(zhuǎn)換方法

    這篇文章主要介紹了python實(shí)現(xiàn)JAVA源代碼從ANSI到UTF-8的批量轉(zhuǎn)換方法,涉及Python針對(duì)文件操作與編碼轉(zhuǎn)換的相關(guān)技巧,需要的朋友可以參考下
    2015-08-08
  • python 消費(fèi) kafka 數(shù)據(jù)教程

    python 消費(fèi) kafka 數(shù)據(jù)教程

    今天小編就為大家分享一篇python 消費(fèi) kafka 數(shù)據(jù)教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-12-12
  • python 基于dlib庫(kù)的人臉檢測(cè)的實(shí)現(xiàn)

    python 基于dlib庫(kù)的人臉檢測(cè)的實(shí)現(xiàn)

    這篇文章主要介紹了python 基于dlib庫(kù)的人臉檢測(cè)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • Java?超詳細(xì)講解核心類Spring?JdbcTemplate

    Java?超詳細(xì)講解核心類Spring?JdbcTemplate

    JdbcTemplate?JdbcTemplate是Spring?JDBC核心包(core)中的核心類,它可以通過(guò)配置文件、注解、Java?配置類等形式獲取數(shù)據(jù)庫(kù)的相關(guān)信息,實(shí)現(xiàn)了對(duì)JDBC開(kāi)發(fā)過(guò)程中的驅(qū)動(dòng)加載、連接的開(kāi)啟和關(guān)閉、SQL語(yǔ)句的創(chuàng)建與執(zhí)行、異常處理、事務(wù)處理、數(shù)據(jù)類型轉(zhuǎn)換等操作的封裝
    2022-04-04
  • Python使用pyh生成HTML文檔的方法示例

    Python使用pyh生成HTML文檔的方法示例

    這篇文章主要介紹了Python使用pyh生成HTML文檔的方法示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-03-03
  • python中zip()函數(shù)遍歷多個(gè)列表方法

    python中zip()函數(shù)遍歷多個(gè)列表方法

    在本篇文章里小編給大家整理的是一篇關(guān)于python中zip()函數(shù)遍歷多個(gè)列表方法,對(duì)此有興趣的朋友們可以學(xué)習(xí)下。
    2021-02-02

最新評(píng)論