網站首頁 編程語言 正文
下面是出現的錯誤解釋
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.勾選才能修改版本,將需要的低版本導入即可:
弄完了之后,重新運行就好。
原文鏈接:https://blog.csdn.net/weixin_42099082/article/details/89365643
相關推薦
- 2024-01-15 mybatis中@Results,@ResultMap注解使用
- 2022-02-10 g2繪制雙折線圖+柱形圖+自定義legend
- 2022-03-27 c++模擬實現string類詳情_C 語言
- 2022-04-15 玩數據必備Python庫之numpy使用詳解_python
- 2022-03-15 使用Sqlyog遠程連接數據庫報錯解決方案_數據庫其它
- 2022-03-15 巧用Redis實現分布式鎖詳細介紹_Redis
- 2022-06-29 Python利用pynput實現劃詞復制功能_python
- 2022-05-22 查看Docker容器的信息的方法實現_docker
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支