【发布时间】: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