Python requests.get() 函数问题 - 无法建立新连接

我编写了一个简单的 python 代码,它从 txt 文件中读取域列表,并根据返回的结果检查每个域是否是 WordPress 站点。


代码如下:


import requests 


#Loop domains list

with open('domains2') as f:

for line in f:

    domain = line

    source = requests.get(domain)

    if "wp-include" in source:

            results = 'Yes'

        else:

                results = 'No'


    print(line , ' : ' , results)

错误如下:


Traceback (most recent call last):

File "./test4.py", line 8, in <module>

source = requests.get(domain)


File "/usr/local/lib/python2.7/dist-packages/requests/api.py", line 75, in get

return request('get', url, params=params, **kwargs)


File "/usr/local/lib/python2.7/dist-packages/requests/api.py", line 60, in request

return session.request(method=method, url=url, **kwargs)


File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 533, in request

resp = self.send(prep, **send_kwargs)


File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 646, in send

r = adapter.send(request, **kwargs)


File "/usr/local/lib/python2.7/dist-packages/requests/adapters.py", line 516, in send

raise ConnectionError(e, request=request)


requests.exceptions.ConnectionError: HTTPConnectionPool(host='testing.com%0a', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fd5a00c4d50>: Failed to establish a new connection: [Errno -2] Name or service not known',))

只有当我手动设置 source 的值如下并且不从列表中读取域并且结果正确时,我才能运行我的代码:


source = requests.get(domain).text 


哔哔one
浏览 390回答 2
2回答

萧十郎

import requests&nbsp;#Loop domains listwith open('domains2') as f:for line in f:&nbsp; &nbsp; domain = line.rstrip()&nbsp; &nbsp; source = requests.get(domain)&nbsp; &nbsp; if "wp-include" in source.text:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; results = 'Yes'&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; results = 'No'&nbsp; &nbsp; print(line , ' : ' , results)source.text 获取请求响应, rstrip() 删除 \n
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python