python - 使用 python 搜索谷歌输出
问题描述
我想在我的 python 程序中包含一个选项来搜索特定城市的时间并获得谷歌在一些不同事物上的输出。
我希望能够通过使用 python 代码获得出现在屏幕顶部的谷歌输出(很多时候它是维基百科或其他页面的输出)。
例如:
我现在如何用 python 复制下午 6:10 的输出?
解决方案
如图所示,url 是 timeanddate.com,我捕获了位置、工作日和时间。我没有展示从网站读取 html 的代码,如果你感兴趣,可以从下面的链接中获取。您需要安装 bs4 和 brotli。
import brotli
from Tool import read_URL
from bs4 import BeautifulSoup
url = "https://www.timeanddate.com/worldclock/"
response, html = read_URL(url)
if html:
soup = BeautifulSoup(html, "html.parser")
tags = soup.find_all('td')
time_dict = {tags[i].text:tags[i+1].text for i in range(0, len(tags), 2)
if tags[i].text.strip() != ''}
# a dictionary created with 'location':'weekday hh:mm' key pairs, like
# 'Washington DC *': 'Tue 13:53'
最好有 time_dict 作为参考,还有你的系统时钟。不是一直从网站获取时间数据。
推荐阅读
- cloudflare - 我可以使用 Cloudflare 从一个网站看起来像多个网站吗?
- c# - 删除后 EF 实体仍然是父集合的一部分
- php - 使用PHP附加数据时如何通过API在谷歌表格中设置复选框?
- cakephp - 控制器没有得到 hasOne 关系数据 - Cakephp
- angular - 从 API 接收数据后的 Ag-grid 网格渲染
- typescript - 我无法在 Protractor 和 Jasmine 中使用 IF 和 EXPECT
- google-apps-script - Google Apps 脚本:如何从文件夹中删除某些文件类型
- angularjs - 如何在按钮单击时获取当前表格行的 html 控件的值?
- python - 有没有办法在 python3 jupyter 笔记本中取消嵌套熊猫数据框?
- javascript - Google AdSense 测试代码未在我的实时网站上显示测试广告