首页 > 解决方案 > 使用 python 搜索谷歌输出

问题描述

我想在我的 python 程序中包含一个选项来搜索特定城市的时间并获得谷歌在一些不同事物上的输出。

我希望能够通过使用 python 代码获得出现在屏幕顶部的谷歌输出(很多时候它是维基百科或其他页面的输出)。

例如:

我现在如何用 python 复制下午 6:10 的输出?

标签: python

解决方案


如图所示,url 是 timeanddate.com,我捕获了位置、工作日和时间。我没有展示从网站读取 html 的代码,如果你感兴趣,可以从下面的链接中获取。您需要安装 bs4 和 brotli。

杰森的工具.py

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 作为参考,还有你的系统时钟。不是一直从网站获取时间数据。


推荐阅读