python音标1003python音标_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("完成,請到相應文件夾查看!")1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
總結
以上是生活随笔為你收集整理的python音标1003python音标_python selenium 爬取百度翻译单词音标的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: GEE面向对象分类(先分割影像 、再计算
- 下一篇: 账户系统的具体实现