无界面(webdriver.PhantomJS)访问有道

#-*-coding:utf-8-*-
#-*-coding:utf-8-*-
#-*-coding:utf-8-*-
from  selenium  import webdriver#网页驱动
import time
from lxml import etree
from  selenium.webdriver.common.keys import Keys
#构建浏览器
driver=webdriver.PhantomJS(executable_path=r'E:\爬虫文件\phantomjs-2.1.1-windows\bin\phantomjs.exe')
url='http://fanyi.youdao.com/'
driver.get(url=url)
#模拟浏览器输入值
#while  True:
word=input('请输入需要被翻译的内容')
# driver.find_element_by_id('inputOriginal').send_keys(Keys.CONTROL,'a')#选中全部内容
# driver.find_element_by_id('inputOriginal').send_keys(Keys.BACK_SPACE)#删除内容
driver.find_element_by_id('inputOriginal').send_keys(word)
driver.save_screenshot('%s.png'%word)
time.sleep(1.8)
# driver.find_element_by_id('transMachine').click()
# time.sleep(3)
content=driver.page_source
tree=etree.HTML(content)
fanyi1=tree.xpath('.//div[@id="transTarget"]//text()')
print(fanyi1)
fanyi=tree.xpath('.//no-link/text()')
print(fanyi)
driver.find_element_by_id('inputOriginal').send_keys(Keys.CONTROL,'a')#选中全部内容
driver.find_element_by_id('inputOriginal').send_keys(Keys.BACK_SPACE)#删除内容
    #break







你可能感兴趣的:(爬虫)