比如在国内其实于很多这样的网站
也不是说非要使用google api
代码:
def make_requests(): response = [None] responseText = None if(request_fanyi_900cha_com(response)): responseText = read_response(response[0]) response[0].close() def read_response(response): if response.info().get('Content-Encoding') == 'gzip': buf = BytesIO(response.read()) return gzip.GzipFile(fileobj=buf).read().decode('utf8') elif response.info().get('Content-Encoding') == 'deflate': decompress = zlib.decompressobj(-zlib.MAX_WBITS) inflated = decompress.decompress(response.read()) inflated += decompress.flush() return inflated.decode('utf8') return response.read().decode('utf8') def request_fanyi_900cha_com(response): response[0] = None try: req = urllib.request.Request("https://fanyi.900cha.com/my/") req.add_header("Connection", "keep-alive") req.add_header("Pragma", "no-cache") req.add_header("Cache-Control", "no-cache") req.add_header("sec-ch-ua", "".Not/A)Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"") req.add_header("sec-ch-ua-mobile", "?0") req.add_header("sec-ch-ua-platform", ""Windows"") req.add_header("Origin", "https://fanyi.900cha.com") req.add_header("Upgrade-Insecure-Requests", "1") req.add_header("DNT", "1") req.add_header("Content-Type", "application/x-www-form-urlencoded") req.add_header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36") req.add_header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9") req.add_header("Sec-Fetch-Site", "same-origin") req.add_header("Sec-Fetch-Mode", "navigate") req.add_header("Sec-Fetch-User", "?1") req.add_header("Sec-Fetch-Dest", "document") req.add_header("Referer", "https://fanyi.900cha.com/my/") req.add_header("Accept-Encoding", "gzip, deflate, br") req.add_header("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8") req.add_header("Cookie", "Hm_lvt_c6638ca3ceb11f7967e468ee00ced931=1657536004; XSRF-TOKEN=eyJpdiI6ImdjZW9OdnJrUU5aMGs0MFJPVHh1RVE9PSIsInZhbHVlIjoiMUl5UThyalJnODlJVlNOaEZYTFM2UnBCejlOVHlmbmpXeGMrb3pkZ0dSM1NNXC9wTHk2N21OdkRCWXFlZm1HMk9JSXE5U003b2ZzVnprWFYrRVRlTlhRPT0iLCJtYWMiOiI5ZmEzOWI0N2RkYzY1YzY4ZTY0NjMxMGUzNjNiYzcxM2U2ZTZmNzg0YTM5MjE2ZWZiMzE4YjU2YjgzMDdmNTIyIn0%3D; 900_session=eyJpdiI6IkNBRVo5S1NuZnZCaGE2N3JxRFdIc2c9PSIsInZhbHVlIjoiY3Z4VzhVb3ZaQ2w1T0dlTUsxeGRHaFVQSFpqZGZ6cCtMS1o0OUtNT3lFNkd6MjNTS0VaSk50eG1uSk1WXC9QUEhXcmlUamRUQnVaMGEwUW5INStCQlVBPT0iLCJtYWMiOiIxYzgwMzc4MjVjYzFkMDkxMGQxNGViNjVmYzQxNDhiOTllMmI1YzQ5NDQyNzZlMzMzMTc5NjVjMGU2NjVhNDVhIn0%3D; Hm_lpvt_c6638ca3ceb11f7967e468ee00ced931=1657536081") body = b"content=%E9%AB%98%E5%93%81%E8%B4%A8%E7%89%9B%E4%BB%94%E7%9F%AD%E8%A3%A4%EF%BC%8C%E7%A0%81%E6%95%B026%E5%88%B030%EF%BC%8C%E7%8B%AC%E7%AB%8B%E5%8C%85%E8%A3%85%EF%BC%8C%E6%95%B0%E9%87%8F700%E6%9D%A1%EF%BC%8C%E5%90%AB%E6%B3%AA%E8%A1%80%E4%BA%8F+%E7%A7%92%E6%9D%80%E6%89%8B%E6%85%A2%E6%97%A0%E3%80%82%E6%AC%BE%E5%8F%B7%EF%BC%9AC130&direction=source&_token=BAZNibcmpTFjYXmYvPplPbxONdN2djwhFuNjKnyd" response[0] = urllib.request.urlopen(req, body) except urllib.error.URLError as e: if not hasattr(e, "code"): return False response[0] = e except: return False return True