日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

Python3--批量爬取数据之调金山词霸api进行翻译

發(fā)布時(shí)間:2025/4/5 python 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python3--批量爬取数据之调金山词霸api进行翻译 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

上代碼:

#/usr/bin/env python3 #coding=utf8from fake_useragent import UserAgent import http.client import hashlib import urllib import random,csv import json,time import requests#獲取IP列表并檢驗(yàn)IP的有效性 def get_ip_list():f=open('IP.txt','r')ip_list=f.readlines()f.close()return ip_list#從IP列表中獲取隨機(jī)IP def get_random_ip(ip_list):proxy_ip = random.choice(ip_list)proxy_ip=proxy_ip.strip('\n')proxies = {'https': proxy_ip}return proxies#注釋:a若查找不到中人名則返回a(row[6]) def getTransResult(q): type = "json"q = q.lower()myurl = 'http://dict-co.iciba.com/api/trans/vip/translate'#q = 'hello baidu trans' #要翻譯的內(nèi)容myurl = "http://dict-co.iciba.com/api/dictionary.php?w="+q+"&type="+type+"&key=key" #開(kāi)發(fā)者Keyip_list = get_ip_list()proxies = get_random_ip(ip_list)headers = { 'User-Agent':str(UserAgent().random)}try:time.sleep(1)req=requests.get(myurl,headers=headers,proxies=proxies)except:print('程序出錯(cuò),暫停20秒')time.sleep(20)proxies = get_random_ip(ip_list)headers = { 'User-Agent':str(UserAgent().random)}req=requests.get(myurl,headers=headers,proxies=proxies)req.encoding="utf-8"data = req.textmresult = json.loads(data)getTransResult=''try:getTransResult = mresult['symbols'][0]['parts'][0]['means'][0]except:return getTransResultprint('翻譯結(jié)果為:'+getTransResult)return getTransResult#功能:讀取文件并處理 def read_file(filepath):reader=[]with open(filepath,'r') as csvfile:spanreader = csv.reader(csvfile,delimiter='|',quoting=csv.QUOTE_MINIMAL)for row in spanreader:if row:reader.append(row)return reader#功能:將爬取到的內(nèi)容寫入文件 #注意事項(xiàng):寫文件時(shí)open中要加上newline='',否則寫一行后程序會(huì)自動(dòng)換行 def write_file(filepath,row):with open(filepath,'a+',encoding='utf-8',newline='') as csvfile:spanreader = csv.writer(csvfile,delimiter='|',quoting=csv.QUOTE_MINIMAL)spanreader.writerow(row)if __name__ == "__main__":reader = read_file('S_baiduBaike_youdaoChinese_utf-8.csv')for row in reader:if not row[6]:print('現(xiàn)在爬取的人名是:'+row[0])TransResult = getTransResult(row[0])if not TransResult.find('[人名]')==-1:TransResult=TransResult.replace('[人名]','')row[6] = TransResultelif not TransResult.find('[男子名]')==-1:TransResult=TransResult.replace('[男子名]','')row[6] = TransResultelif not TransResult.find('[女子名]')==-1:TransResult=TransResult.replace('[女子名]','')row[6] = TransResultwrite_file('經(jīng)有道金山詞霸爬取后/S_baiduBaike_youdaoChinese_jscb.csv',row)print('程序運(yùn)行結(jié)束')

總結(jié)

以上是生活随笔為你收集整理的Python3--批量爬取数据之调金山词霸api进行翻译的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。