# -*- 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()