jared_shop_item_list.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # -*- coding: utf-8 -*-
  2. from selenium import webdriver
  3. from selenium.webdriver.common.action_chains import ActionChains
  4. from selenium.webdriver.common.keys import Keys
  5. from selenium.webdriver.support import expected_conditions as EC
  6. from selenium.webdriver.support.wait import WebDriverWait
  7. from selenium.webdriver.common.by import By
  8. from bs4 import BeautifulSoup
  9. from utility import database_access as DA
  10. from utility.parseutils import *
  11. from utility.connect import *
  12. import dataset
  13. import sys
  14. from datetime import datetime
  15. import pandas as pd
  16. import time
  17. import json
  18. import re
  19. def brower_start(port):
  20. options = webdriver.ChromeOptions()
  21. # browser = webdriver.Chrome(options=options)
  22. # 上面成功再來用docker
  23. browser = webdriver.Remote(
  24. command_executor='http://127.0.0.1:'+str(port)+'/wd/hub',
  25. desired_capabilities=options.to_capabilities()
  26. )
  27. return browser
  28. def get_url_list(driver):
  29. for i in range(5, 43, 2):
  30. try:
  31. wait = WebDriverWait(driver, 60)
  32. wait.until(
  33. EC.element_to_be_clickable((By.XPATH, '//*[@id="pane"]/div/div[1]/div/div/div[2]/div[1]/div[{}]/div/a'.format(i)))
  34. )
  35. driver.find_element(By.XPATH,'//*[@id="pane"]/div/div[1]/div/div/div[2]/div[1]/div[{}]/div/a'.format(i)).send_keys(Keys.DOWN)
  36. time.sleep(1)
  37. except:
  38. pass
  39. url_soup = BeautifulSoup(driver.page_source, 'html.parser')
  40. url_list = []
  41. for i in url_soup.find_all('a'):
  42. try:
  43. if i['href'].find('maps/place') != -1:
  44. url_list += [[i['href'], i['aria-label']]]
  45. except:
  46. pass
  47. return url_list
  48. def keyin_keyword(driver, keyword):
  49. button = driver.find_element_by_id("searchbox")
  50. driver.implicitly_wait(30)
  51. ActionChains(driver).move_to_element(button).send_keys(keyword).send_keys(Keys.RETURN).perform()
  52. time.sleep(3)
  53. def main():
  54. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/google_poi?charset=utf8mb4')
  55. keyword = '青年旅館'
  56. if len(sys.argv) >1:
  57. keyword=sys.argv[1]
  58. port=4444
  59. if len(sys.argv) >2:
  60. port=int(sys.argv[2])
  61. print('drvier start...')
  62. driver = brower_start(port)
  63. num=0
  64. cursor=db.query('select num from progress_list where kw = "'+keyword+'"')
  65. for c in cursor:
  66. num=c['num']
  67. break
  68. table2=db['progress_list']
  69. cursor=db.query('select * from lat_lon_loc where num >= '+str(num))
  70. # cursor=db.query('select * from lat_lon_loc')
  71. lst=[]
  72. for c in cursor:
  73. lst.append({'num':c['num'],'loc':c['loc'],'lat':c['lat'],'lon':c['lon']})
  74. for r in lst:
  75. latitude = r['lat'] #緯度
  76. longitude = r['lon'] #精度
  77. table2.upsert({'kw':keyword,'num':r['num']},['kw'])
  78. url = 'https://www.google.com.tw/maps/@{},{},15z?hl=zh-TW'.format(latitude, longitude)
  79. driver.get(url)
  80. keyin_keyword(driver, keyword)
  81. for page in range(4):
  82. print( r['loc'], latitude, longitude, page)
  83. url_list = get_url_list(driver)
  84. print(url_list)
  85. shop_item_list_col = ['name','lon','lat','keyword','item_url','crawler_date']
  86. for item in url_list:
  87. result = [item[1], longitude, latitude, keyword, item[0], datetime.today().strftime("%Y/%m/%d %H:%M")]
  88. print(result)
  89. # insert_sql = """INSERT IGNORE INTO {}{} VALUES {}"""\
  90. # .format('shop_item_list', str(tuple(shop_item_list_col)).replace('\'',''), tuple(result))
  91. # DA.mysql_insert_data(db, insert_sql)
  92. if page < 2 :
  93. element = driver.find_element_by_id('ppdPk-Ej1Yeb-LgbsSe-tJiF1e')
  94. driver.implicitly_wait(30)
  95. ActionChains(driver).move_to_element(element).click(element).perform()
  96. if __name__ == '__main__':
  97. main()