谷歌地图翻译英文地名

# -*- encoding: utf-8 -*-
import pymysql
# import pymssql
import re
from urllib.parse import quote,unquote
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from time import sleep


class sqlServerClient(object):
    def __init__(self):
        # self.host = host
        # self.port = port
        # self.user = user
        # self.pwd = pwd
        # self.dbname = dbname
        self.connect = pymysql.connect(host="localhost", port=3306, user="root", passwd="zxc", db="country_status_city", charset='utf8')  # mysql连接
        self.cursor = self.connect.cursor()

    def selectData(self):
        try:
            self.cursor.execute("select id, name from states")
            results = self.cursor.fetchall()
            num = 0

            for row in results:
                id = row[0]
                state = row[1]
#                 print("州(省,道):", state)
                # 开始请求
                getUrl = req_url + state
                browser.get(getUrl)
                sleep(5)

                url = browser.current_url
                url = url.replace("http://www.google.cn/maps/place/", "")
                pattern = re.compile('/.*$')
                res = re.sub(pattern, "", url)
                res = unquote(res, 'utf-8')
                print(res)
                self.updateDB(id, res)
        except Exception as error:
            print("错误信息:", error.__str__())
            self.connect.rollback()
        pass

#     def insertData(self, title, content, pubtime, pubunit, insert_time, es_timestamp):
#         try:
#             self.cursor.execute(
#                 """INSERT INTO material(MID, title, content, status, pubtime, note, pubunit, create_time, update_time) VALUES \
#                 (default, %s, %s, '未办理', %s, '业务数据样本', %s,  %s, getdate())""",
#                 (title, content, pubtime, pubunit, insert_time))
#             self.connect.commit()
#         except Exception as error:
#             print("错误信息:", error.__str__())
#             self.connect.rollback()

    def updateDB(self, id, state_chinese):
        try:
            self.cursor.execute("""update states set name_chinese = (%s) where id = (%s)""", (state_chinese, id))
            self.connect.commit()
            print(id, "update ok")
        except Exception as error:
            print("update错误:", error.__str__())
            self.connect.rollback()

    def close(self):
        self.cursor.close()
        self.connect.close()


if __name__ == "__main__":
    req_url = "http://www.google.cn/maps/place/"
    chrome_options = Options()
    # 设置chrome浏览器无界面模式
    chrome_options.add_argument('--headless')
    browser = webdriver.Chrome(options=chrome_options)

    client = sqlServerClient()
    client.selectData()
    client.close()

    # 关闭浏览器
    browser.close()
    # 关闭chromedriver进程
    browser.quit()

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值