为什么Phantomjs在虚拟机访问默认去访问127.0.0.1不能正确获取到数据?

收藏
PhantomJS
6
Feb 1, 2018

代码如下:

driver.get('https://www.baidu.com')

虚拟机中在用selenium调用phantomjs的时候访问百度的时候出现了默认去访问127.0.0.1,出现如下错误:

NoSuchElementException: Message: {"errorMessage":"Unable to find element with id 'kw'","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"83","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:47518","User-Agent":"Python-urllib/2.7"},"httpVersion":"1.1","method":"POST","post":"{\"using\": \"id\", \"sessionId\": \"885d2b10-e3c1-11e7-ad6c-d752f7862175\", \"value\": \"kw\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/885d2b10-e3c1-11e7-ad6c-d752f7862175/element"}}
Screenshot: available via screen

 

回答

Bravian回答

原因

 phantomjs内部域名解析问题, 没有找到具体原因

解决方法

1. 可以直接通过访问百度的域名ip地址直接获取资源 如上图

2. 通过soocket 模块逆向解析获取域名ip 再通过得到的ip地址来获取资源

import socket
cur_ip = socket.gethostbyname('baidu.com')

 

(0)

提交成功