我正在做一个从游戏媒体网站中提取文章的项目,并且我正在做一个基本的测试运行,根据 VSCode 的调试器,在我设置多线程提取之后始终挂起(更改线程数无济于事)在两个站点上。老实说,我不确定我在这里做错了什么;我按照已经列出的示例进行操作。其中一个站点 Gamespot 甚至被用于某人的教程中,我尝试删除另一个站点(Polygon),但似乎没有帮助。我已经创建了一个虚拟环境,并在 Python 3.8 和 3.7 中都进行了尝试。所有依赖似乎都得到满足;我还在 repl dot it 中进行了测试并得到了相同的挂起。
我很想听到我只是做错了什么,所以我可以修复它;我真的很想在这些特定的网站和他们的文章上做一些数据科学!但似乎,至少对于 OS X 用户来说,多线程存在某种错误。这是我的代码:
#import system functions
import sys
import requests
sys.path.append('/usr/local/lib/python3.8/site-packages/')
#import basic HTTP handling processes
#import urllib
#from urllib.request import urlopen
#import scraping libraries
#import newspaper and BS dependencies
from bs4 import BeautifulSoup
import newspaper
from newspaper import Article
from newspaper import Source
from newspaper import news_pool
#import broad data libraries
import pandas as pd
#import gaming related news sources as newspapers
gamespot = newspaper.build('https://www.gamespot.com/news', memoize_articles=False)
polygon = newspaper.build('https://www.polygon.com/gaming', memoize_articles=False)
#organize the gaming related news sources using a list
gamingPress = [gamespot, polygon]
print("About to set the pool.")
#parallel process these articles using multithreading (store in mem)
news_pool.set(gamingPress, threads_per_source=4)
print("Setting the pool")
news_pool.join()
print("Pool set")
#create the interim pandas dataframe based on these sources
final_df = pd.DataFrame()
#a limit on sources could be placed here; intentionally I have placed none
limit = 10
for source in gamingPress:
#these are temporary placeholder lists for elements to be extracted
list_title = []
list_text = []
list_source = []
count = 0
for article_extract in source.articles:
article_extract.parse()
#further limit functionality could be placed here; not placed
if count > limit:
break
list_title.append(article_extract.title)
list_text.append(article_extract.text)
list_source.apprend(article_extract.source_url)
print(count)
count +=1 #progress the loop *via* count
temp_df = pd.DataFrame({'Title': list_title, 'Text': list_text, 'Source': list_source})
#Append this to the final DataFrame
final_df = final_df.append(temp_df, ignore_index=True)
#export to CSV, placeholder for deeper analysis/more limited scope, may remain
final.df.to_csv('gaming_press.csv')
当我最终放弃并在控制台打断时,这就是我得到的回报:
About to set the pool.
Setting the pool
^X^X^CTraceback (most recent call last):
File "scraper1.py", line 31, in <module>
news_pool.join()
File "/usr/local/lib/python3.8/site-packages/newspaper3k-0.3.0-py3.8.egg/newspaper/mthreading.py", line 103, in join
self.pool.wait_completion()
File "/usr/local/lib/python3.8/site-packages/newspaper3k-0.3.0-py3.8.egg/newspaper/mthreading.py", line 63, in wait_completion
self.tasks.join()
File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/queue.py", line 89, in join
self.all_tasks_done.wait()
File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/threading.py", line 302, in wait
waiter.acquire()
KeyboardInterrupt