将数据写入MongoDB数据库

import requests
from lxml import etree
import pymongo


class article(object):
    def __init__(self):
        # 创建连接,ip地址和端口号
        self.client = pymongo.MongoClient("iP地址", 端口号)
        # 连接的数据库
        db = self.client.数据库的名字
        # 账号密码的认证
        db.authenticate("账号", "密码")
        # 连接的表
        self.conn_table = db.表名

    def write_to_mongodb(self):
        url = 'http://caipiao.163.com/help/14/0805/21/A2TOI4UF00754IHE_3.html'
        response = requests.get(url)
        content = response.content.decode('gb2312')
        tree = etree.HTML(content)
        # 转为字符串
        p_list = tree.xpath('//div[@class="help_t_int"]/p')
        text = ''
        for num in range(len(p_list)):
            conten = etree.tostring(p_list[num], encoding="utf-8",pretty_print=True,
                                    method="xml").decode('utf-8')
            text += conten

        data = {
            'article_name': '易乐11选5奖项规则',
            'article_sub_name': '',
            'lottery_name': '易乐11选5',
            'article_tag': '',
            'article_class_name': '奖项规则',
            'article_recommend': '',
            'article_from': '',
            'article_from_url': url,
            'article_author': '',
            'article_content': text,
            'article_abstract': '',
            'article_img': '',
            'is_delete': 0
        }
        self.conn_table.insert(data)


article().write_to_mongodb()

 

©️2020 CSDN 皮肤主题: 创作都市 设计师:CSDN官方博客 返回首页