kitUIN/PicImageSearch

httpx google error

Peloxerat opened this issue · 2 comments

Your google module has not been working for a long time. I changed the ports in the proxy, even removed it, but the error still appears. When requesting, it gives the same error as in #122. To launch I used https://github.com/kitUIN/PicImageSearch/blob/main/demo/en/demo_google.py.
I tried to write my own code and it worked right away.

import httpx
from bs4 import BeautifulSoup
from urllib.parse import urlencode

HEADERS = {
    "User-Agent": "Mozilla/5.0 (Linux; Android 6.0.1; SM-G920V Build/MMB29K) "\
                  "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.98 Mobile Safari/537.36"
}

def reverse(image_url):
    try:
        # Uncomment the following lines if you want to validate the image extension
        '''
        image_extension = image_url.rsplit('.', 1)[1].lower()
        if image_extension not in ["jpg", "jpeg", "png", "gif", "bmp"]:
            return {"error": "Invalid image URL"}
        '''

        params = {"safe": "off", "sbisrc": "tg", "image_url": image_url}
        url = f"https://images.google.com/searchbyimage?{urlencode(params)}"
        
        with httpx.Client(follow_redirects=True) as client:
            response = client.get(url, headers=HEADERS)
            response.raise_for_status()

        soup = BeautifulSoup(response.text, 'html.parser')
        result = {"result_text": "", "origin": response.text}
        # for example
        output_div = soup.select_one("div.r5a77d")
        if output_div:
            decoded_text = output_div.get_text()
            
            result["result_text"] = decoded_text
        else:
            return {"error": "Failed to find text output"}

        return result
    except httpx.RequestError as error:
        return {"error": f"Failed to reverse image: {str(error)}"}

# For testing:
if __name__ == "__main__":
    test_url = "https://raw.githubusercontent.com/kitUIN/PicImageSearch/main/demo/images/test03.jpg"
    result = reverse(test_url)
    print(result)

Based on the code you provided, I only noticed that the params and HEADERS are different, the other parts seem to have no difference from those in the module.
Moreover, I have tested it in multiple network environments and cannot reproduce the network errors you and #122 mentioned.
Perhaps more details are needed.

Based on the code you provided, I only noticed that the params and HEADERS are different, the other parts seem to have no difference from those in the module. Moreover, I have tested it in multiple network environments and cannot reproduce the network errors you and #122 mentioned. Perhaps more details are needed.

For some reason, when I was connected to the DNS cloudflare, it didn’t work for me. Changed DNS - everything worked