多线程爬取都挺好链接并保存到mongodb

  • 一个比较简单,python3多线程使用requests库爬取都挺好,并使用正则提取下载链接,保存到MongoDB
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
@author:Aiker Zhao
@file:doutinghao.py
@time:下午8:18
"""
import requests
import re
import pymongo
from multiprocessing import Pool

MONGO_URL = 'localhost:27017'
MONGO_DB = 'doutinghao'
MONGO_TABLE = 'doutinghao'
client = pymongo.MongoClient(MONGO_URL, connect=False)
db = client[MONGO_DB]

def get_result(url):
    response = requests.get(url).text
    # print(reponse.text)
    pattern = re.compile('', re.S)
    result = re.findall(pattern, response)
    if result:
        for i in result:
            url, name = i
            yield {
                "name": name,
                'url': url
            }

def save_to_mongo(result):
    if db[MONGO_TABLE].insert(result):
        print('存储到MongoDB成功', result)
        return True
    return False

def main(result):
    # result = get_result(url)
    save_to_mongo(result)

if __name__ == '__main__':
    pool = Pool()
    url = "https://www.xl720.com/thunder/34283.html"
    item = [item for item in get_result(url)]
    # print(item)
    pool.map(main, item)
    pool.close()
    pool.join()

多线程爬取都挺好链接并保存到mongodb

目前创新互联建站已为上1000家的企业提供了网站建设、域名、网站空间网站托管运营、企业网站设计、泰安网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

多线程爬取都挺好链接并保存到mongodb
多线程爬取都挺好链接并保存到mongodb


名称栏目:多线程爬取都挺好链接并保存到mongodb
文章位置:http://ybzwz.com/article/gogooc.html