[关闭]
@shaobaobaoer 2018-02-12T08:20:33.000000Z 字数 2784 阅读 927

PYTHON 爬虫 ———— Tarot Reader Google Tanslation

塔罗 tarot 爬虫


0x01 Abstract

This is the advanced part of my previous article "Tarot Reader Data Crawling"

In this article I will introduce how to crawl the data from google translation and renew my database. To be honst, I refer to many referrences and choosing requests lib and execJS as the engine of my worm.

0x02 To handle TK arguement

TK arguement is google's algorithm to transffer the target language to a complex string of number which hide in get request.

Thanks a billion for cocoa520 's script for calculating the TK arguement in Google translation.
and here is the link which makes a great convinence in crawling the translated article
LINK

To change it to python code . I recommend execJS . U can get it by pip install execJS.

  1. import execjs
  2. class Py4Js():
  3. def __init__(self):
  4. self.ctx = execjs.compile("""
  5. #copy JS here
  6. """)
  7. def getTk(self,text):
  8. return self.ctx.call("TL",text)

0x03 Start The Translation

Besides the tk arguement, other arguement seems to be simple. Use your burpsuit to catch the request and reloaded it by Python.

  1. import requests
  2. from HandleJs import Py4Js # cocoa520's script means HandleJS
  3. def translate(tk,content):
  4. if len(content) > 4891:
  5. print("[-] too long for translation")
  6. return
  7. param = {'tk': tk, 'q': content}
  8. result = requests.get("""http://translate.google.cn/translate_a/single?client=t&sl=en
  9. &tl=zh-CN&hl=zh-CN&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss
  10. &dt=t&ie=UTF-8&oe=UTF-8&clearbtn=1&otf=1&pc=1&srcrom=0&ssel=0&tsel=0&kc=2""", params=param)
  11. #Get the json list;
  12. for text in result.json():
  13. print(text)
  14. def main():
  15. js = Py4Js()
  16. content=" To be or not to be ,that is a question."
  17. tk = js.getTk(content)
  18. translate(tk,content)
  19. if __name__ == "__main__":
  20. main()

0x04 Handling the result.

However the result is not clear enough to save in database.
To handle it and select Chinses sentence, u need another function.

  1. RE = (result.json()[0])
  2. result = ""
  3. for i in RE:
  4. if type(i[0]) == type("i"):
  5. result += i[0].split()[0].strip("\n")
  6. print(type(result))
  7. return result

0x05 Save Them In database !

OK! Here is the last step.
Combine these functions and save the result in database, make sure that the charset is UTF-8.

  1. def translate_main_function():
  2. for table_name in big_arcana_dict.values():
  3. for id in "123456":
  4. database_translate_content(table_name, id)
  5. def database_insert_translate(tables_name, id, text):
  6. """
  7. I write the result in file, to ensure the speed.
  8. """
  9. sql = "UPDATE %s SET cn_data='%s' WHERE id=%s;" % (tables_name, text, id)
  10. f = open("sql.txt", "a")
  11. f.write(sql + "\n" + "COMMIT;" + "\n")
  12. def database_translate_content(table, id):
  13. db = pymysql.connect("localhost", "root", "", "tarot")
  14. cursor = db.cursor()
  15. sql = "SELECT * from %s WHERE id=%s LIMIT 0,1" % (table, id)
  16. # print(sql)
  17. try:
  18. cursor.execute(sql)
  19. results = cursor.fetchall()
  20. for row in results:
  21. text = row[1]
  22. text = (T_tool(text))
  23. #print(text)
  24. database_insert_translate(table, id, text)
  25. except:
  26. print("Error: unable to fetch data")
  27. db.close()

Before running the code , renew the tarot database and create a new column to save translation version.

0x06 To Download The Newest Code!

The whole code u can enter my github to view or copy.
Github href

Just for learning . Not for commerce

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注