python 线程池 分配任务给线程,爬取加入库。

import requests
from guanjianzi import keylist as keys
import re
from conMySql import ConDb
from multiprocessing.dummy import Pool as ThreadPool
s=requests.Session()
con=ConDb()
def getlist(url):
    html=s.get(url).content.decode()
    res=r'<li  class="li"><font class="date">(.*?)</font><a href="(.*?)" target="_blank">(.*?)</a><span class="new"></span></li>'
    li=re.findall(res,html)
    title=re.findall(r"<title>(.*?)</title>",html)[0]
    for x,y,z in li:
        y=url+str(y)
        y=re.sub(r"\/index\.html\.",'',y)
        html1=s.get(y).content.decode()
        text=str(html1)
        guanjianzi = {}
        num = 0
        for key in keys:  # 循环遍历关键字列表,查询关键字出现的次数
            count = text.count(key)  # count 关键字在本文中出现的次数
            if count > 0:
                guanjianzi.update({key: count})  # 把关键字和出现的次数添加到字典中
                num += count
        print(title,y,z,guanjianzi,num,x)
        sql='''  insert into urllist(source,urls,titles,keyname,keysum,date1) values('{}','{}','{}',"{}",'{}','{}') '''.format(title,y,z,guanjianzi,num,x)
        con.runSql(sql)
if __name__ == '__main__':
    urls = ['http://www.ndrc.gov.cn/xwzx/xwfb/index.html','http://www.ndrc.gov.cn/zwfwzx/zxgg/index.htmm','http://www.ndrc.gov.cn/zwfwzx/xzxknew/index.html','http://www.ndrc.gov.cn/zcfb/zcfbl/index.html','http://www.ndrc.gov.cn/zcfb/gfxwj/index.html','http://www.ndrc.gov.cn/zcfb/zcfbgg/index.html','http://www.ndrc.gov.cn/zcfb/zcfbghwb/index.html','http://www.ndrc.gov.cn/zcfb/zcfbtz/index.html','http://www.ndrc.gov.cn/zcfb/jd/index.html','http://www.ndrc.gov.cn/yjzq/index.html']
    t=ThreadPool(5)
    for url in urls:
        t.apply_async(getlist,args=(url,))
    t.close()
    t.join()
    sql1='select max(bat) from urllist limit 1'
    bat=con.runSql(sql1)[0][0]
    bat=int(bat)+1
    # print(bat)
    sql2="update urllist set bat='{}'".format(bat)
    con.runSql(sql2)

猜你喜欢

转载自blog.csdn.net/u012593871/article/details/79045451
今日推荐