PowerBi利用Python Script绕过ODBC来导入MongoDB数据

 

 

转载于:https://www.cnblogs.com/Javi/p/10324633.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个比较复杂的问题,我会尽量帮你解答。 首先,我们需要使用 Python 爬取奥运会数据。可以用 requests 库向目标网站发送 HTTP 请求,然后用 BeautifulSoup 库解析 HTML 页面,从而获取需要的数据。具体操作步骤如下: ```python import requests from bs4 import BeautifulSoup # 发送 HTTP 请求获取 HTML 页面 url = 'https://olympics.com/tokyo-2020/olympic-games/en/results/all-sports/medal-standings.htm' response = requests.get(url) # 解析 HTML 页面获取需要的数据 soup = BeautifulSoup(response.text, 'html.parser') table = soup.find('table', class_='medal-standing-table') rows = table.find_all('tr') for row in rows[1:]: cols = row.find_all('td') country = cols[1].text.strip() gold = cols[2].text.strip() silver = cols[3].text.strip() bronze = cols[4].text.strip() print(country, gold, silver, bronze) ``` 上面的代码可以爬取奥运会奖牌榜的数据,并打印出来。接下来,我们需要将获取的数据导入MongoDB 数据库中。可以使用 PyMongo 库来连接 MongoDB,然后将数据插入到指定的集合中。具体操作步骤如下: ```python import pymongo # 连接 MongoDB 数据库 client = pymongo.MongoClient('mongodb://localhost:27017/') db = client['olympics'] medal_collection = db['medal'] # 插入数据到集合中 for row in rows[1:]: cols = row.find_all('td') country = cols[1].text.strip() gold = cols[2].text.strip() silver = cols[3].text.strip() bronze = cols[4].text.strip() medal_collection.insert_one({ 'country': country, 'gold': gold, 'silver': silver, 'bronze': bronze }) ``` 上面的代码可以将奥运会奖牌榜的数据插入到 MongoDB 的 medal 集合中。接下来,我们可以通过 PyMongo 库提供的 API 来实现增删查改等操作。具体操作步骤如下: ```python # 查询所有数据 cursor = medal_collection.find() for document in cursor: print(document) # 查询指定条件的数据 query = {'country': 'China'} cursor = medal_collection.find(query) for document in cursor: print(document) # 更新指定条件的数据 query = {'country': 'China'} new_values = {'$set': {'gold': '38'}} medal_collection.update_one(query, new_values) # 删除指定条件的数据 query = {'country': 'China'} medal_collection.delete_one(query) ``` 上面的代码分别演示了查询所有数据、查询指定条件的数据、更新指定条件的数据和删除指定条件的数据的操作。 希望这些代码可以帮助你完成 Python 爬取奥运会数据导入 MongoDB 进行增删查改等功能的任务。如果还有其他问题,请随时提出。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值