clickbot_100_one.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import random
  2. import sys
  3. import dataset
  4. from selenium import webdriver
  5. from selenium.webdriver.chrome.service import Service
  6. from selenium.webdriver.common.by import By
  7. import traceback
  8. import datetime
  9. import codecs
  10. import time
  11. import urllib
  12. import argparse
  13. import logging
  14. import sys
  15. from logging.handlers import SysLogHandler
  16. import socket
  17. import pandas as pd
  18. import socket
  19. import os
  20. import dataset
  21. import pymysql
  22. pymysql.install_as_MySQLdb()
  23. driver = None
  24. def restart_browser():
  25. global driver
  26. user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'
  27. s = Service('/Users/mac/Downloads/127/chromedriver')
  28. options = webdriver.ChromeOptions()
  29. options.add_argument('--headless')
  30. # options.add_argument('--remote-debugging-port=9222')
  31. # options.add_experimental_option("debuggerAddress", "192.168.192.45:9922")
  32. options.add_argument("--user-agent=" + user_agent)
  33. options.add_argument("--incognito")
  34. # options.add_argument('--proxy-server=socks5://172.104.93.163:41800')
  35. driver = webdriver.Chrome(options=options, service=s)
  36. str1 = driver.capabilities['browserVersion']
  37. driver.delete_all_cookies()
  38. driver.set_window_size(1400, 20000)
  39. return driver
  40. def process_one():
  41. lst=['信義房屋','信義 房屋','信義房仲','信義 房仲']
  42. # lst=["驊揚室內裝修設計","驊揚室內裝修設計 評論"]
  43. # lst=['真理大學','真理大學停招','真理大學倒閉','真理大學評價','真理大學倒閉dcard']
  44. # lst = ['百威旅行社','百威旅遊','百威旅行']
  45. # lst = ['信義 房屋']
  46. # lst = ['加百裕工業股份有限公司','加百裕','黃靖容']
  47. # lst = ['富玉珠寶','富玉珠寶有限公司']
  48. # lst = ['台北牙周病醫生推薦 ','微創水雷射','水雷射牙周病']
  49. # lst = ['真理大學 site:ptt.cc','真理大學 site:dcard.tw']
  50. # lst = ['高雄 全 戶 軟水','高雄 淨水器推薦','淨水系統 高雄']
  51. # lst = ['啟翔輕金屬','啟翔','陳百欽']
  52. date='0731'
  53. for term in lst:
  54. driver=restart_browser()
  55. escaped_search_term=urllib.parse.quote(term)
  56. # googleurl = 'https://www.google.com/search?q={}&num={}&hl={}&gl=tw'.format(escaped_search_term, 100,'zh-TW')
  57. googleurl = 'https://www.google.com/search?q={}&num={}&hl={}&gl=tw&tbm=vid&tbs=vd:m'.format(urllib.parse.quote(term), 100, 'zh-TW')
  58. driver.get(googleurl)
  59. time.sleep(6)
  60. print(driver.current_url)
  61. # driver.save_screenshot('/Users/mac/Downloads/'+date+term+'.png')
  62. driver.save_screenshot('/Users/mac/Downloads/'+date+term+"_vi"+'.png')
  63. df=pd.DataFrame()
  64. # elmts=driver.find_elements(By.XPATH,"//div[@class='yuRUbf']//a")
  65. elmts=driver.find_elements(By.XPATH,"//div[@class='xe8e1b']//a")
  66. print(len(elmts))
  67. cnt=1
  68. datadict={'搜尋詞':[],'結果標題':[],'結果網址':[],'結果名次':[]}
  69. for elmt in elmts:
  70. try:
  71. href=elmt.get_attribute('href')
  72. datadict['搜尋詞'].append(term)
  73. datadict['結果標題'].append(elmt.text)
  74. datadict['結果網址'].append(href)
  75. datadict['結果名次'].append(str(cnt))
  76. cnt+=1
  77. except:
  78. print('href2 exception')
  79. traceback.print_exc()
  80. if len(datadict['結果標題'])<=0:
  81. print('None')
  82. driver.quit()
  83. sys.exit()
  84. df['搜尋詞']=datadict['搜尋詞']
  85. df['結果標題']=datadict['結果標題']
  86. df['結果網址']=datadict['結果網址']
  87. df['結果名次']=datadict['結果名次']
  88. # df.to_csv('/Users/mac/Downloads/'+date+term+".csv", encoding='utf-8')
  89. df.to_csv('/Users/mac/Downloads/'+date+term+"_vi"+".csv")
  90. driver.quit()
  91. print('等待')
  92. time.sleep(random.randint(80,90))
  93. process_one()