【问题标题】:google reverse image url display in python谷歌在python中显示反向图像url
【发布时间】:2022-01-20 13:25:25
【问题描述】:

我编写了 python 代码来使用一些 google dork 关键字在 google 中搜索图像。代码如下:

def showD(self):

    self.text, ok = QInputDialog.getText(self, 'Write A Keyword', 'Example:"twitter.com"')

    if ok == True:
        self.google()

def google(self):
    filePath = self.imagePath
    domain = self.text
    searchUrl = 'http://www.google.com/searchbyimage/upload'
    multipart = {'encoded_image': (filePath, open(filePath, 'rb')), 'image_content': '', 'q': f'site:{domain}'}
    response = requests.post(searchUrl, files=multipart, allow_redirects=False)
    fetchUrl = response.headers['Location']
    webbrowser.open(fetchUrl)


App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())

我只是不知道如何在我的程序中显示搜索结果的 url。我试过这段代码:

import requests
from bs4 import BeautifulSoup
import re

query = "twitter"
search = query.replace(' ', '+')
results = 15
url = (f"https://www.google.com/search?q={search}&num={results}")

requests_results = requests.get(url)
soup_link = BeautifulSoup(requests_results.content, "html.parser")
links = soup_link.find_all("a")

for link in links:
    link_href = link.get('href')
    if "url?q=" in link_href and not "webcache" in link_href:
        title = link.find_all('h3')

        if len(title) > 0:
            print(link.get('href').split("?q=")[1].split("&sa=U")[0])
            # print(title[0].getText())
            print("------")

但它只适用于普通的谷歌搜索关键字,当我尝试针对谷歌图片搜索的结果优化它时失败了。它没有显示任何结果。

【问题讨论】:

  • 这个库对你有帮助吗? pypi.org/project/googlesearch-python
  • @JosipDomazet 不,我的朋友,我缺少的是 for 循环中的一个小技巧
  • 这里:如果 link_href 中的 "url?q=" 而不是 link_href 中的 "webcache":title = link.find_all('h3')
  • 您能否澄清一下“搜索结果的网址”是什么?不只是您的 fetchUrl 吗?或者您的意思是所有只指向结果但想要排除其他像页脚/页眉中的 URL 的 URL?
  • 就像你在谷歌中搜索图片时我需要提取结果的网址

标签: python


【解决方案1】:

目前没有简单的方法可以使用普通的 HTTPS 请求来抓取 Google 的“按图片搜索”。在响应此类请求之前,他们可能会使用几种复杂的技术检查用户是否真实。即使您的代码工作示例也无法长时间运行——它恰好在 20-100 次请求后被 Google 禁止。

所有真正用图像抓取 Google 的 Python 公开解决方案都使用 Selenium 并模仿真实的用户行为。所以你可以自己走这条路。 python-selenium binding 的界面并不难适应,可能除了设置过程。

根据我的口味,其中最好的是hardikvasa/google-images-download(Github 上有 7.8K 星)。不幸的是,这个库没有像图像路径或二进制格式的图像这样的输入接口。它只有 similar_images 参数,它需要一个 URL。不过,您可以尝试将其与http://localhost:1234/... URL 一起使用(您可以轻松设置一个this way)。

您可以查看all these questions 并查看所有解决方案都使用 Selenium 来完成此任务。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-09
    • 2011-01-17
    • 2018-11-02
    • 1970-01-01
    相关资源
    最近更新 更多