当前位置:   article > 正文

最新有道翻译爬虫教程 (python爬虫)_有道爬虫

有道爬虫

环境:python3

直接上代码:User-Agent 填自己的

  1. #coding=utf-8
  2. import requests
  3. import json
  4. from lxml import etree
  5. class YouDaoTranslateWeb:
  6. def __init__(self):
  7. self.url="http://www.youdao.com/w/eng/{}/#keyfrom=dict2.index"
  8. self.headers = {"User-Agent": "xxx"}
  9. def run(self):
  10. user_input = input("请输入要翻译的内容:")
  11. self.url = self.url.format(user_input)
  12. response = requests.get(url=self.url,headers=self.headers)
  13. strs = response.content.decode()
  14. self.html = etree.HTML(strs)
  15. def get_result(self):
  16. if len(self.html.xpath("//div[@id='results']//div[@id='fanyiToggle']")) > 0 :
  17. result = self.html.xpath("//div[@id='results']//div[@id='fanyiToggle']//p/text()")
  18. result = result[1] if len(result) > 0 else None
  19. elif len(self.html.xpath("//div[@id='results']//div[@class='trans-container']//p/span/a/text()")) > 0:
  20. result = self.html.xpath("//div[@id='results']//div[@class='trans-container']//p/span/a/text()")
  21. result = result[0] if len(result) > 0 else None
  22. else:
  23. results = self.html.xpath("//div[@id='scontainer']//div[@class='title']/span/text()")
  24. results.pop()
  25. result=""
  26. for i in results:
  27. result+=i.strip()+"\r\n"
  28. print("翻译结果是:")
  29. print(result)
  30. if __name__ == '__main__':
  31. while True:
  32. baidu = YouDaoTranslateWeb()
  33. baidu.run()
  34. baidu.get_result()

2.其它翻译

最新百度翻译爬虫 获取sign(python爬虫) 最新百度翻译爬虫 获取sign(python爬虫)

最新百度翻译爬虫 手机版(python爬虫) 最新百度翻译爬虫 手机版(python爬虫)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/855653
推荐阅读
相关标签
  

闽ICP备14008679号