使用 PhantomJs selenium 解析爬取有道翻译

from selenium import webdriver
import time
from lxml import etree
from selenium.webdriver.common.keys import Keys

youdao_url = 'http://fanyi.youdao.com/'

driver = webdriver.PhantomJS(executable_path=r'D:\PhantomJS\phantomjs-2.1.1-windows\bin\phantomjs.exe')

driver.get(youdao_url)

while True:
    word = input('输入单词:')

    driver.find_element_by_id("inputOriginal").send_keys(word)

    time.sleep(4)

    driver.find_element_by_id('transMachine').click()

    driver.save_screenshot('翻译.png')

    tree = etree.HTML(driver.page_source)

    fanyi = tree.xpath('.//div[@class="input__target__dict"]/div[@class="dict__relative"]/span/text()')

    print(fanyi)

 

你可能感兴趣的:(python)