Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 优化此for loop以提高速度的最佳方法是什么?(熊猫、BS对象。)_Python_Pandas_Beautifulsoup - Fatal编程技术网

Python 优化此for loop以提高速度的最佳方法是什么?(熊猫、BS对象。)

Python 优化此for loop以提高速度的最佳方法是什么?(熊猫、BS对象。),python,pandas,beautifulsoup,Python,Pandas,Beautifulsoup,我有一个熊猫数据框架/系列,每行都有一个URL,可以从中创建一个漂亮的Soup对象并从中获取数据。但是,循环速度非常慢,运行我当前的循环需要很多小时才能完成,因为有超过21000行。这可以通过apply或numpy进行优化吗 count = 0 url = data['review_link'] for link in url: url = "http://{}".format(link) req = requests.get(url) best

我有一个熊猫数据框架/系列,每行都有一个URL,可以从中创建一个漂亮的Soup对象并从中获取数据。但是,循环速度非常慢,运行我当前的循环需要很多小时才能完成,因为有超过21000行。这可以通过apply或numpy进行优化吗

count = 0
url = data['review_link']


for link in url:
    url = "http://{}".format(link)
    req = requests.get(url)
    best = BeautifulSoup(req.text, 'html')
    
    reviews = best.find_all('div', {'class' : 'review-detail'})
    
    for review in reviews:

        entry = {
            'artist' : safeFindText(review, 'ul', {'class' : 'artist-links artist-list single-album-tombstone__artist-links'}),
            'bnm' : safeFindText(review, 'p', {'class' : 'bnm-txt'}),
            'title' : safeFindText(review, 'h1', {'class' : 'single-album-tombstone__review-title'}),
            'score' : safeFindText(review, 'span', {'class' : 'score'}),
            'label' : safeFindText(review, 'li', {'class' : 'labels-list__item'}),
            'year' : safeFindText(review, 'span', {'class' : 'single-album-tombstone__meta-year'}),
            'author' : safeFindText(review, 'a', {'class' : 'authors-detail__display-name'}),
            'author_title' : safeFindText(review, 'span', {'class' : 'authors-detail__title'}),
            'genre' : safeFindText(review, 'a', {'class' : 'genre-list__link'}),
            'review_timestamp' : safeFindText(review, 'time', {'class' : 'pub-date'}),
            'review_abstract' : safeFindText(review, 'div', {'class' : 'review-detail__abstract'}),
            'review_text' : safeFindText(review,'div', {'class' : 'clearfix flex-md'})
            
            
                                   }
        pitchfork_dicts.append(entry)
        if count % 10 == 0:
            print("{} rows completed".format(count))
    count += 1

问题是,当请求等待响应时,程序处于空闲状态。由于等待网络IO可能是下一步,我不认为apply或numpy会给您带来太多的速度。我能想到的两种优化方法是,要么使用一个线程池来处理所有的请求,而主循环则在它的列表中穿行。或者使用async异步发出请求,以便在等待响应时,您可以继续处理列表


对于线程池,您可以创建一个主线程推送到的线程安全队列。然后是一个线程池,线程池弹出下一个项目,发出请求并处理结果。

实际的循环可能不是瓶颈,
。apply
不会产生明显的差异,事实上,它可能会稍微慢一点
req=requests.get(url)
可能是瓶颈,而不是在数据帧上循环。或者是
safeFindText
中发生的任何事情。这里也不清楚您将如何使用
numpy
,看起来您只是从网站中提取文本数据,
numpy
用于表示多维数组上数值运算的代码,线性代数等等如果这是您的瓶颈,您可以同时向API发出多个请求。