猿问

无法将数据插入 MongoDb:

无法在变量的帮助下将数据插入 mongodb 但如果我打印输出并将其粘贴到 db.collection.insert_many(output) 则代码运行正常并存储数据但我直接使用 db.collection.insert_many(output_final) 它给我错误。请帮忙。


import re

import json

from selenium import webdriver

from bs4 import  BeautifulSoup

import requests

from fake_useragent import  UserAgent

import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")

db = myclient["db_db"]



ua          = UserAgent()

header      = {'user-agent':ua.chrome}

driver      = webdriver.Chrome('C:/Users/MUNTAZIR/Downloads/Compressed/chromedriver_win32/chromedriver.exe')

driver.get('https://www.eduvision.edu.pk/scholarships/index.php?authority=1&level=4&field=1&cat=2&type=1')

# tr = driver.find_element_by_id('ctl00_ctl42_g_7f68baae_5353_4bdd_bfe1_b88e3367234f_csr1_table')

soup = BeautifulSoup(driver.page_source, 'lxml')

scholar = soup.findAll("div", {"class": "card-content col-xs-12"})

s_output1 = ""

for s in scholar[0:1]:

        title=s.findAll("h2")[0].text

        desc = s.findAll("div", {"class": "text"})[0].text.replace("\n", "").replace('"','')

        url = "https://www.eduvision.edu.pk/scholarships/" + s.a['href']

        type= "Higher Education Commission"

        # print(type +"\n" +title +"\n" +desc +"\n" +url +"\n")

        s_output1 = ("{""\n"

             '"type"' + ":" + '" ' + type + ' ",' + "\n"

             '"title"' + ":" + '" ' + title + ' ",' + "\n"

             '"url"' + ":" + '" ' + url + ' ",' + "\n"

             '"description"' + ":" + '" ' + desc + ' "' + "\n"

             "}""\n"

             )


郎朗坤
浏览 267回答 1
1回答
随时随地看视频慕课网APP

相关分类

Python
我要回答