ææ¾ç¶åéä¸é£åçå°ç·¨ï¼è² 責å°ç²çµ²åçæç« å»£ç¼å°åå社åï¼éåéç¨æ¯ç¥ä¸ä¹å³ï¼å°±æ¯ä¸ç´ä¸æ·å°æå享社åãæä¸å¤©å¤§ç´è¦å享70å¤å社åï¼å æ¤æ¯å¤©å¾è±ä¸å°æå·¦å³ä¾åéé ãæ©æ¢°å¼ãçå·¥ä½ã
éåéç¨å¤§é¨åæ¯èãå°ç·¨ç¥å¨ï¼IGæå¿å¤§æ¾éï¼seleniumå¨IGä¸ç實åãé¡ä¼¼ï¼å æ¤è¥æ¨å·²ç¶è®é該ç¯æç« ï¼å¤§å¤é¨åæ¯å¯ä»¥æ²¿ç¨çã
æ´å實ä½åªéè¦ç¨å°Seleniumå¥ä»¶ï¼å æ¤è¥æ¯ä½¿ç¨Anacondaï¼å°±ç´æ¥å¨Anaconda promptä¸è¼¸å ¥ä»¥ä¸æ令ï¼è¥å¨ç³»çµ±ç°å¢ä¸å®è£pythonï¼åç´æ¥å¨Terminalè²¼ä¸æ令å³å¯ã
pip install selenium
é»æä¸è¼phantomjs phantomjsæ¯Seleniumçæ§å¶å¨ï¼çæ¼æ¯å°pythonç¨å¼ç¢¼è®ææ©å¨äººçå·¥å ·ãSeleniumä¹æ以è½ç¬å°è¨±å¤ç¬è²ç¬ä¸å°ç網ç«ï¼å°±æ¯å çºSeleniumæ¯æ¨¡ä»¿äººçè¡çºï¼ççæéä¸åç覽å¨ï¼ä¸åå³çµ±çç¬è²ï¼ç´æ¥è«æ±å°å ä¸ä¾è§£æãç¶ç¶éåææ缺é»Seleniumçå·è¡æçç¸å°å°±å·®å¾å¤ï¼ä½é¢å°IGé種ãåæ å¼ãç網ç«ï¼å¿ é è¦ç¨Seleniumæè½éçå°ã
ä¸å系統ï¼å示å¯è½æä¸å¤ªä¸æ¨£ï¼ä½ä¸å½±é¿åè½ï¼è®è ä¸å¿ é度æå¿ã
é»æä¸è¼chromedriver chromedriveræ¯å°é給ç¨å¼æ§å¶çç覽å¨ãå æ¤å¯¦ä½æï¼ç¨å¼æ幫æ¨éåä¸åchromeç覽å¨ï¼ä¸¦ä¸æç §ç¨å¼èªåææéï¼ç¶ç¶ä¹å¯ä»¥äººå·¥ä»å ¥æä½ãchromedriverå¿ é èæ¨ç®åçchromeçæ¬ç¸åï¼è¥æ¨ä¸ç¥éæ¨chromeççæ¬ï¼å¯ä»¥é»é裡æ¥çéä¸è¼ççæ¬ï¼ç¢ºå®å¾ï¼å³å¯é»ä¸æ¹é¸æä¸è¼æªæ¡ã
phantomjsèchromedriverä¸è¼å®æå¾ï¼é½å¿ é æ¾å°ä¸»ç¨å¼çè³æ夾ä¸ï¼å¦ä¸åå³ï¼ï¼è®Python確ä¿è½æå°éå ©åæªæ¡ãè¥ç³»çµ±çºlinuxï¼å¯è½ææ使ç¨chromedriverèphantomjsæªæ¡æ¬éåé¡ï¼å æ¤å¿ é è¦è¼¸å ¥ä»¥ä¸ä¾æéæ¬éï¼777çºå ¨éï¼è¥æ¯è¦ºå¾æå®å ¨çæ ®è å¯ä»¥èªè¡èª¿æ´ï¼ã
æ¨å¯è½æ好å¥ï¼çºä½éè¦ç¹å¥æºåä¸åFB帳èï¼å ¶å¯¦ä½¿ç¨seleniumç¼ææ¯æä¸å®é¢¨éªçï¼è¥åä½å¤ªéæ¼æ©æ¢°å¼ï¼æ被FBç¦æ¢ç¼æï¼å°åä¸å¤©ï¼å¤å以ææè¨ç®ï¼çè³å°æå¾æä¸æåæ¬é½èªªä¸å®ãç±æ¤åå ï¼éæ¯å»ºè°å人使ç¨çFB帳èä¸è¦æ¿ä¾å»£ç¼ï¼ç¶ç¶æ¨ç¾å¨è®æ¬ç¯æç« ï¼ä¸¦ä¸é²è¡çæ«ç實ä½ç·´ç¿ï¼æ¯ä¸è³æ¼è¢«FBç¦æ¢ç¼æï¼ä½é·æé使ç¨ï¼å°±ææ帳èå°éçæ ®ã
å¦ä¸ååå æ¯ï¼å ¬ç§ç¡éåæãå çºå¨æ¨çå人帳èä¸ï¼é½æ¯ä½ 身éçç實æåï¼æ¨æåå ç社åï¼å¯è½æ¯æ¨åå°ãåä¸ãé«ä¸ççç´ç¤¾åï¼ç¸ä¿¡æ¨ç¥éå¨éäºç¤¾åç¼é廣åæç« ï¼æé æç麼åå½å§ï¼
è«è¼¸å ¥æ¨æ ¾ç¼æç帳èå¯ç¢¼ãcatchåæ¸æ¯è¼¸å ¥æ¨å¾ ææ³è¦å享ç社åï¼èç¶ç¶ä¸¦ä¸æ¯æ社åå ¨åï¼èæ¯ç¨æ¨¡ç³æå°çæ¹å¼ï¼å æ¤å¦ä¸ç¯ä¾ä¸è¼¸å ¥ã廣å|è¡é·ãï¼å æ¤å¾ æè¥æ¨å å ¥ç社åå稱ä¸æã廣åãææ¯ãè¡é·ãï¼å°±æé²è¡å享ã
useEmail='æ¨çFB帳è'
usePass='æ¨çFBå¯ç¢¼'
catch ='廣å|è¡é·'
postURL = 'https://www.facebook.com/permalink.php?story_fbid=134084921578475&id=101285581525076'
postURLåæ¸é常好ç解ï¼å°±æ¯æ¨æ³è¦ç¼æç該ç¯FBæç« ï¼èåå¾è©²ç¯FBæç« ç網åæ¹å¼é常簡å®ï¼åªéè¦é»æ該æç« çãæéãï¼å³å¯åå¾è©²æç« ç網åã
éåé¨ååªéè¦æ³¨æï¼phantomjsèchromedriveréå ©åæªæ¡çæºæ¾ä½ç½®ï¼ç¢ºå¯¦æ¯èç¨å¼ç¢¼å¨åä¸å±¤ç®éå³å¯ï¼ä¸¦ä¸å·¥ä½ç®éè¨å®ç¡èª¤ãè¥ä»¥ä¸çç¡åé¡ï¼é£ä¾¿å¯ä»¥ç´æ¥é å©å·è¡éå»ï¼æéç¨å¼æ§å¶çç覽å¨ã
# è¨å®åºæ¬åæ¸
desired_capabilities = DesiredCapabilities.PHANTOMJS.copy()
#æ¤èå¿
é ææèªå·±é»è
¦çUser-Agent
desired_capabilities['phantomjs.page.customHeaders.User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36'# PhantomJS driver è·¯å¾ ä¼¼ä¹åªè½çµå°è·¯å¾
driver = webdriver.PhantomJS(executable_path = 'phantomjs', desired_capabilities=desired_capabilities)# éééç¥æé
chrome_options = webdriver.ChromeOptions()
prefs = {'profile.default_content_setting_values.notifications' : 2}
chrome_options.add_experimental_option('prefs',prefs)
# 以ä¸ä¸å註解æéï¼ç覽å¨å°±ä¸æéå
# chrome_options.add_argument(' â headless')
# chrome_options.add_argument(' â no-sandbox')
# chrome_options.add_argument(' â disable-dev-shm-usage')# éåç覽å¨
driver = webdriver.Chrome('chromedriver',chrome_options=chrome_options)
è¥æ¨åºç¾é¯èª¤è¨æ¯ï¼
WebDriverException: 'phantomjs' executable needs to be in PATH.
代表é»è ¦æ¾ä¸å°æ¨çphantomjsæªæ¡ï¼æ¨å¿ é éæ°ç¢ºèªæªæ¡æ¾ç½®çä½ç½®æ¯å¦æ£ç¢ºã
è¥é¯èª¤è¨æ¯æ¯ï¼
WebDriverException: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
åæ¯æ¾ä¸å°chromedriveræªæ¡ï¼ä¸æ¨£å¿ é éæ°ç¢ºèªæªæ¡æ¾ç½®çä½ç½®æ¯å¦æ£ç¢ºã
æå¾è¥é¯èª¤è¨æ¯çºï¼
SessionNotCreatedException: session not created: This version of ChromeDriver only supports Chrome version 76
åæ¯ä»£è¡¨æ¨æä¸è¼çchromedriverèæ¨çç覽å¨çæ¬ä¸¦ä¸ç¬¦åï¼è«éæ°æ¥çæ¨çé»è ¦çæ¬ï¼ä¸¦ä¸æ¾å°å°æçä¸è¼æªæ¡ã
ç±æ¼æ£é¢FBçæ¹çææï¼å æ¤æäºäººçFBæ¯èçï¼æäºåæ¯æ°çï¼å æ¤æå°å ©ç¨®çæ¬ä»é¢çç»å ¥æ¹å¼é½å¯«ä¸å»ã
####### éå§æä½ è¼¸å
¥å¸³èå¯ç¢¼ç»å
¥ å°fbé¦é #######
driver.get('http://www.facebook.com')
time.sleep(1)
assert 'Facebook' in driver.title
try:
elem = driver.find_element_by_id('email')
elem.send_keys(useEmail)
elem = driver.find_element_by_id('pass')
elem.send_keys(usePass)
elem.send_keys(Keys.RETURN)
except:
driver.find_element_by_xpath('//*[@name="email"]')
elem.send_keys(useEmail)
elem = driver.find_element_by_xpath('//*[@name="pass"]')
elem.send_keys(usePass)
elem.send_keys(Keys.RETURN)
time.sleep(5)
è¥ç»å ¥é裡æäºä¸å¯ææå ç´ ï¼åç½®ç¡æ³èªååç»å ¥ï¼ä¹å¯ä»¥ç¨æåæåçæ¹å¼ç»å ¥ãæ¬æå¼µæ³è¦ç¯ççæéï¼æ¯å¾é¢éè¤ç¼æçæéï¼å æ¤åé¢çç»å ¥ä¸¦ç¡å¤ªå¤§å½±é¿ï¼é¤éè¦å°éåæ©å¨äººæååã
éååä½æ¯çºäºçä¸æè¦è½è·³æ¨çææ社åé é¢ï¼é²è¡æååä½ãéåIDå ¶å¯¦å°±æ¯æ¨FBé¦é 網åä¸ï¼æå¾é¢çé£ä¸²æ¸åï¼å æ¤è¥æ¨æ³è¦çç¥éå ©è¡çæ¥é©ï¼ä¹å¯ä»¥ç´æ¥å¯«æ»éé åæ¸yourIDã
#åå¾å¸³èçµå°ID
try:#èç
getID = driver.find_element_by_xpath('//*[@title="å人æªæ¡"]').get_attribute('href')
version='old'
except:#æ°ç
getID = driver.find_element_by_xpath('//div[@data-pagelet="LeftRail"]/div/ul/li/div/a').get_attribute('href')
version='new'
yourID = getID[getID.find('id=')+3:]
é裡è¥æ¨ç帳èæä¿®æ¹éIDï¼å°éäºæ¸åIDæ¹æè±æï¼é£å°±ä¸é©ç¨æ¬æç« çç¨å¼ç¢¼ã
ç¶åå¾æ¨çIDå¾ï¼å°±æåæé é¢å°æ¨ææç社åé é¢ï¼ç®çæ¯çºäºæåææ社åçå稱ã
# åæå°èªå·±ç社å
driver.get('https://www.facebook.com/profile.php?id=' + yourID + '&sk=groups')
ç±æ¼FBæ¯åæ å¼ç¶²é ï¼å æ¤å¿ é å°é é¢æ»¾å°åºé¨å¾ï¼ææç¢çæ°çè³æãæç« ï¼å æ¤é裡å©ç¨JavaScriptçèªæ³window.scrollToï¼ä¾èªå滾åé é¢ï¼ç´å°æ»¾å°åºé¨çºæ¢ã
# 滾åé é¢for i in range(30): driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") time.sleep(1)
é¦å å æåææ社åãé裡ä¸æ¨£æ¯å çºæ°èççåå¥ï¼èæä¸åçç¬åæ¹å¼ã
# åå¾ææ社åif len(driver.find_elements_by_xpath('//*[@data-hovercard]')) == 0: #æ°ç getallgroup = driver.find_elements_by_xpath('//a/span[@dir="auto"]')else: #èç getallgroup = driver.find_elements_by_xpath('//*[@data-hovercard]')
éè¨å¾æåå åæè¨å®catchåæ¸ï¼è¥è©²ç¤¾åå稱ä¸ï¼æåºç¾catchæè¨å®çééµåï¼å°±åå ¥å¾ æè¦ç¼éçåå®ãéé åè½ä¸æ¹é¢è½é¿å æ¨ç¼éå°ä¸äºç§äººãä¸æ³æ廣åç社åï¼å¦ä¸æ¹é¢ï¼åæ¯å¯ä»¥ä¾ç §ä¸åçæç« ï¼é²è¡ä¸å種é¡ç社åé²è¡ç¼éï¼è¬å¦ï¼éèé¡çæç« ï¼å°±è¨å®è¡ç¥¨ãæ貨ç¸éçééµå社åç¼éã
get_groups = []
#檢æ¥ï¼è¥ç¡è¦æ±å°±ç¤¾åå
¨é¨ç¼
if len(catch) == 0:
for groups in getallgroup:
get_groups.append(groups.text)
else:
for groups in getallgroup:
if len("".join(re.findall(catch, groups.text))) >0:
get_groups.append(groups.text)
é裡æä¸åå°å¿å¾ãä¸è¦å°çç¼éå°ä¸äºè«åå ¶å¦ç廣å社åï¼éå ¶å¯¦å°ä¸åç²çµ²åçæé·ï¼æå¤çæå¾å¤§çä½ç¨ã
é裡就æ¯æ¨¡ä»¿äººçæ¥é©ï¼é»é¸å享ãç¼é社åçãçºäºè®æ´ååä½çèµ·ä¾ãå人ãï¼å¨æ¯ååä½é½å ä¸äºtime.sleepç延é²ï¼ä¸¦ä¸æ¯ä½¿ç¨äºæ¸çæ¹å¼ã
####### éå§å享 #######
# åæå°æç«
driver.get(postURL)
if version == 'new':
#æ°ç
for send in get_groups:
# æä¸å享
driver.find_element_by_xpath('//*[@aria-label="å¯ééå給æåæ張貼å¨ä½ çåæ
æå ±ä¸ã"]').click()
time.sleep(random.randint(3,5))
# æä¸å享å°ç¤¾å
driver.find_element_by_xpath("//*[contains(text(), 'å享å°ç¤¾å')]").click()
time.sleep(random.randint(2,7))
for send in get_groups:
# 輸å
¥ç¤¾åå稱
driver.find_element_by_xpath('//*[@aria-label="æå°ç¤¾å"]').send_keys(send)
time.sleep(random.randint(2,7))
driver.find_elements_by_xpath('//div[@aria-labelledby]')[-1].click()
length = len(driver.find_element_by_xpath('//*[@aria-label="æå°ç¤¾å"]').get_attribute('value')) #æå°æ¬²åªé¤çæ¬ä½æ¸é
driver.find_element_by_xpath('//*[@aria-label="æå°ç¤¾å"]').send_keys(length * Keys.BACKSPACE) #æ¡å¹¾æ¬¡åªé¤
time.sleep(random.randint(60,120))
else:
#èç
for send in get_groups:
# æä¸å享
driver.find_element_by_xpath('//*[@title="å¯ééå給æåæ張貼å¨ä½ çåæ
æå ±ä¸ã"]').click()
time.sleep(random.randint(3,5))
# æä¸å享å°ç¤¾å
driver.find_element_by_xpath('//*[contains(text(), "å享å°ç¤¾å")]').click()
time.sleep(random.randint(2,7))
# 輸å
¥ç¤¾åå稱
driver.find_element_by_xpath('//input[@placeholder="社åå稱"]').send_keys(send)
time.sleep(random.randint(2,7))
# æä¸åä¸å»ºé¸æ第ä¸åæç¸ä¼¼ç社å
driver.find_element_by_xpath('//input[@placeholder="社åå稱"]').send_keys('\ue015')
# æä¸enterè¿´è»å»º
driver.find_element_by_xpath('//input[@placeholder="社åå稱"]').send_keys('\ue007')
# å
å«åå§è²¼ææå¾
driver.find_element_by_class_name('_55sg').click()
time.sleep(random.randint(7,15))
# ç¼å¸
driver.find_elements_by_xpath("//button[contains(text(), 'ç¼ä½')]")[-1].click()
time.sleep(random.randint(60,120))
driver.quit()
注æï¼ æ¬æç´å±¬å¸ç¿æ§è³ªï¼å æ¤è«æ¨æ£ç¶ä½¿ç¨ã
作者:楊超霆 行銷搬進大程式 創辦人