爬虫新闻+进度条打印

爬虫新闻+进度条打印

def read_file(path1):
    res = []
    for ea in list(glob(r'{}'.format(path1))):
        print(ea)
        sheet = pd.read_csv(ea,encoding='utf-8')
        res.append(sheet) 
    
    res = pd.concat(res,ignore_index=True)

    return res
import inspect
def getnews(url):
    import numpy as np
    from newspaper import Article
    news = Article(str(url),language='zh')
    news.download()
    try:
        news.parse()
        return news.text
    except:
        return np.nan

with open(f'./tmp_func.py','w') as file:
    file.write(inspect.getsource(getnews).replace(getnews.__name__,"task"))
from tmp_func import task
import tqdm

def get_newspaper_mp(path1,path2):
    sheet = read_file(path1)
    
    n_cores = mp.cpu_count()
    p = mp.Pool(processes = n_cores-2)
    urls = sheet['采集网址'].tolist()
    #tqdm库下的tqdm方法:tqdm.tqdm(iterable,total)
    res = list(tqdm.tqdm(p.imap(task,urls),total = len(urls)))
    p.close()
    p.join()
    
    sheet['内容'] = res
    
    sheet.to_csv(r'{}'.format(path2),encoding='utf-8')
    
    return sheet
from glob import glob 
import pandas as pd
import numpy as np
from newspaper import Article
import threading 
import numba  
import multiprocessing as mp 
import tqdm

if __name__ == '__main__':
    
    path1 = 'D:\\导师任务\\2016-2021新闻\\爬取新闻test\\test*.csv'
    path2 = 'D:\\导师任务\\2016-2021新闻\\test新闻.csv'
    
    get_newspaper_mp(path1,path2)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值