Python3--批量爬取数据之调金山词霸api进行翻译
2018-03-30 13:18
681 查看
上代码: #/usr/bin/env python3
#coding=utf8
from fake_useragent import UserAgent
import http.client
import hashlib
import urllib
import random,csv
import json,time
import requests
#获取IP列表并检验IP的有效性
def get_ip_list():
f=open('IP.txt','r')
ip_list=f.readlines()
f.close()
return ip_list
#从IP列表中获取随机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' #要翻译的内容
myurl = "http://dict-co.iciba.com/api/dictionary.php?w="+q+"&type="+type+"&key=key" #开发者Key
ip_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('程序出错,暂停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.text
mresult = json.loads(data)
getTransResult=''
try:
getTransResult = mresult['symbols'][0]['parts'][0]['means'][0]
except:
return getTransResult
print('翻译结果为:'+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
#功能:将爬取到的内容写入文件
#注意事项:写文件时open中要加上newline='',否则写一行后程序会自动换行
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('现在爬取的人名是:'+row[0])
TransResult = getTransResult(row[0])
if not TransResult.find('[人名]')==-1:
TransResult=TransResult.replace('[人名]','')
row[6] = TransResult
elif not TransResult.find('[男子名]')==-1:
TransResult=TransResult.replace('[男子名]','')
row[6] = TransResult
elif not TransResult.find('[女子名]')==-1:
TransResult=TransResult.replace('[女子名]','')
row[6] = TransResult
write_file('经有道金山词霸爬取后/S_baiduBaike_youdaoChinese_jscb.csv',row)
print('程序运行结束')
#coding=utf8
from fake_useragent import UserAgent
import http.client
import hashlib
import urllib
import random,csv
import json,time
import requests
#获取IP列表并检验IP的有效性
def get_ip_list():
f=open('IP.txt','r')
ip_list=f.readlines()
f.close()
return ip_list
#从IP列表中获取随机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' #要翻译的内容
myurl = "http://dict-co.iciba.com/api/dictionary.php?w="+q+"&type="+type+"&key=key" #开发者Key
ip_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('程序出错,暂停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.text
mresult = json.loads(data)
getTransResult=''
try:
getTransResult = mresult['symbols'][0]['parts'][0]['means'][0]
except:
return getTransResult
print('翻译结果为:'+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
#功能:将爬取到的内容写入文件
#注意事项:写文件时open中要加上newline='',否则写一行后程序会自动换行
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('现在爬取的人名是:'+row[0])
TransResult = getTransResult(row[0])
if not TransResult.find('[人名]')==-1:
TransResult=TransResult.replace('[人名]','')
row[6] = TransResult
elif not TransResult.find('[男子名]')==-1:
TransResult=TransResult.replace('[男子名]','')
row[6] = TransResult
elif not TransResult.find('[女子名]')==-1:
TransResult=TransResult.replace('[女子名]','')
row[6] = TransResult
write_file('经有道金山词霸爬取后/S_baiduBaike_youdaoChinese_jscb.csv',row)
print('程序运行结束')
相关文章推荐
- Python3--批量爬取数据之调用百度api进行翻译
- 【Python】Python的urllib、urllib2模块调用“百度翻译”API进行批量自动翻译
- 利用python调用elasticsearch-api来分析数据并作图进行日报邮件发送
- 利用python调用elasticsearch-api来分析数据并作图进行日报邮件发送
- 利用Python调用ECMWF欧洲中心API批量下载数据
- 使用python及百度API对百度poi数据进行爬取(一)
- 利用python调用elasticsearch-api来分析数据并作图进行日报邮件发送
- 利用python调用elasticsearch-api来分析数据并作图进行日报邮件发送
- CNTK API文档翻译(8)——使用Pandas和金融数据进行时序数据基本分析
- 利用python调用elasticsearch-api来分析数据并作图进行日报邮件发送
- 利用python调用elasticsearch-api来分析数据并作图进行日报邮件发送
- 利用python调用elasticsearch-api来分析数据并作图进行日报邮件发送
- 利用python调用elasticsearch-api来分析数据并作图进行日报邮件发送
- 利用python调用elasticsearch-api来分析数据并作图进行日报邮件发送
- 使用 Google API 进行翻译的 Python 小工具
- python 调用Google Translate API进行翻译
- 利用python调用elasticsearch-api来分析数据并作图进行日报邮件发送
- 使用 Google API 进行翻译的 Python 小工具
- 使用python及百度API对百度poi数据进行爬取(三)
- 利用python调用elasticsearch-api来分析数据并作图进行日报邮件发送