狂想曲丶 发表于 2021-9-22 12:54:22

Python简单爬虫报错

源代码
import requests
header = {'User-Agent':'Mozilla/5.0' ,}
url = 'https://fanyi.baidu.com/sug'
def fanyi(value):
    key = { 'kw': value}
    r = requests.post(url, data = key, headers = header)
    result = r.json()
    return result
if __name__ == '__main__':
    word = input("请输入要翻译的内容: ")
    res = fanyi(word)
    print(res)

请输入要翻译的内容: china

报错信息:
Traceback (most recent call last):
File "C:/Users/98511/PycharmProjects/pythonProject2/demo.py", line 11, in <module>
    res = fanyi(word)
File "C:/Users/98511/PycharmProjects/pythonProject2/demo.py", line 6, in fanyi
    r = requests.post(url, data = key, headers = header)
File "C:\Users\98511\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\api.py", line 117, in post
    return request('post', url, data=data, json=json, **kwargs)
File "C:\Users\98511\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
File "C:\Users\98511\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\sessions.py", line 542, in request
    resp = self.send(prep, **send_kwargs)
File "C:\Users\98511\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\sessions.py", line 655, in send
    r = adapter.send(request, **kwargs)
File "C:\Users\98511\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\adapters.py", line 439, in send
    resp = conn.urlopen(
File "C:\Users\98511\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 696, in urlopen
    self._prepare_proxy(conn)
File "C:\Users\98511\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 964, in _prepare_proxy
    conn.connect()
File "C:\Users\98511\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connection.py", line 359, in connect
    conn = self._connect_tls_proxy(hostname, conn)
File "C:\Users\98511\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connection.py", line 500, in _connect_tls_proxy
    return ssl_wrap_socket(
File "C:\Users\98511\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\util\ssl_.py", line 453, in ssl_wrap_socket
    ssl_sock = _ssl_wrap_socket_impl(sock, context, tls_in_tls)
File "C:\Users\98511\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\util\ssl_.py", line 495, in _ssl_wrap_socket_impl
    return ssl_context.wrap_socket(sock)
File "C:\Users\98511\AppData\Local\Programs\Python\Python38\lib\ssl.py", line 500, in wrap_socket
    return self.sslsocket_class._create(
File "C:\Users\98511\AppData\Local\Programs\Python\Python38\lib\ssl.py", line 997, in _create
    raise ValueError("check_hostname requires server_hostname")
ValueError: check_hostname requires server_hostname

在我这台电脑上始终报错,同样的代码在我同学那里就可以正常输出翻译内容

suchocolate 发表于 2021-9-22 13:22:53

我运行你的代码也正常输出,没有报错。是不是requests版本不同导致的?
不行的话,你把headers加个host试试:
header = {'User-Agent': 'Mozilla/5.0', 'host': 'fanyi.baidu.com'}

狂想曲丶 发表于 2021-9-22 15:51:14

suchocolate 发表于 2021-9-22 13:22
我运行你的代码也正常输出,没有报错。是不是requests版本不同导致的?
不行的话,你把headers加个host试 ...

加了host还是不行,应该不是requests版本的问题,今天中午12点才刚装的,会不会和python版本有关?我的是3.8的

leafu 发表于 2021-9-22 15:55:52

这个其实跟选用的python版本的关系不大,主要原因是因为每次使用 pip install 命令下载插件的时候,下载的都是最新的版本,比如下载requests插件,它会自动的将依赖的urllib3这个插件也安装,然后依赖的插件版本太高,就导致了这个报错的问题。---搬运至CSDN,原文链接如下:
https://blog.csdn.net/liboshi123/article/details/116078509

狂想曲丶 发表于 2021-9-22 16:37:47

leafu 发表于 2021-9-22 15:55
这个其实跟选用的python版本的关系不大,主要原因是因为每次使用 pip install 命令下载插件的时候,下载的 ...

十分感谢兄弟分享文章,问题已经解决了
页: [1]
查看完整版本: Python简单爬虫报错