python selenium 爬取百度翻译单词音标
python selenium 小爬虫
主要流程 读取excel文件中的单词——利用selenium 去百度翻译中获取单词对应的音标——写入cvs文件
selenium 安装 环境配置略过
谷歌浏览器打开百度翻译并等待baidu_translate_input加载完成
browser = webdriver.Chrome()
url = “https://fanyi.baidu.com/?aldtype=85#en/zh/”
browser.get(url)
WebDriverWait(browser, 1000).until(EC.presence_of_all_elements_located((By.ID, ‘baidu_translate_input’)))
打开excel文件,并获取单词sheet的 行数
excelfile = xlrd.open_workbook(r’F:\studytest\word.xlsx’)
sheet = excelfile.sheet_by_name(“单词”)
cnt = sheet.nrows
csv文件写入标题
with open(r’F:\studytest\result.csv’, ‘a’, encoding=‘utf-8’,newline=’’) as csvfile:
writer = csv.writer(csvfile)
writer.writerow((“单词”, “音标”))
定位baidu_translate_input并输入单词
browser.find_element_by_id(‘baidu_translate_input’).send_keys(mystr)
点击翻译
browser.find_element_by_id(‘translate-button’).click()
获取音标
phonetic = browser.find_element_by_class_name(‘dictionary-spell’).text # 音标
全部代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @version : Python 3.7.3
# @Time : 2019/7/24 20:13
import xlrd
import time
import csv
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
browser = webdriver.Chrome()
url = "https://fanyi.baidu.com/?aldtype=85#en/zh/"
browser.get(url)
WebDriverWait(browser, 1000).until(EC.presence_of_all_elements_located((By.ID, 'baidu_translate_input')))
excelfile = xlrd.open_workbook(r'F:\studytest\word.xlsx')
sheet = excelfile.sheet_by_name("单词")
cnt = sheet.nrows
with open(r'F:\studytest\result.csv', 'a', encoding='utf-8',newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(("单词", "音标"))
for i in range(cnt):
mystr = sheet.cell(i, 0).value
browser.find_element_by_id('baidu_translate_input').send_keys(mystr)
browser.find_element_by_id('translate-button').click()
WebDriverWait(browser, 1000).until(EC.presence_of_all_elements_located((By.CLASS_NAME, 'trans-left')))
try:
words = browser.find_element_by_class_name('strong').text # 单词
phonetic = browser.find_element_by_class_name('dictionary-spell').text # 音标
print("%s %s" % (words, phonetic))
data = (words, phonetic)
with open(r'F:\studytest\result.csv', 'a', encoding='utf-8', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(data)
except:
pass
time.sleep(1)
browser.find_element_by_id('baidu_translate_input').clear()
browser.close()
browser.quit()
print("完成,请到相应文件夹查看!")
上一篇: 爬虫_2