使用Python自动化LinkedIn连接
在当今世界中,LinkedIn连接是IT专业人士生活中非常重要的一部分。用户需要向很多其他可能有用的人发送连接请求,但是逐个发送请求可能会耗费时间且繁琐。有没有更好的办法可以让自动化系统来完成这些工作呢?
在本教程中,我们将学习如何使用Python生成一个自动接受LinkedIn连接请求的自动化系统。
所需模块
- Selenium: Selenium不是Python的内置模块。要安装selenium模块,可以使用以下命令:!pip3 install selenium
- Pyautogui: 与selenium一样,pyautogui也不是Python的内置模块。要安装此模块,可以使用以下命令:!pip3 install pyautogui
- Chrome web driver: 我们可以从官方网站下载Chrome web driver。
在实施之前,我们应该首先导入所有模块。
from selenium import webdriver as wd
from selenium.webdriver.common.keys import Keys as ky
import pyautogui as pyt
现在,让我们来编写主函数-
def main():
# url of LinkedIn,com
url_1 = "http://linkedin.com/"
# url of network page of linkedin.com
network_url1 = "http:/linkedin.com/my_network/"
# path for browser web driver
driver_1 = wd.Chrome('C:\Program Files\Web Driver\chromedriver.exe'')
driver_1.get(url_1)
# Drivers code
if __name__ == __main__:
main()
现在,我们需要去身份验证页面,然后进行登录。
代码:
def login_1():
# First, we will get the login element
username_1 = driver.find_element_by_id("login-email")
# then, we will send the keys for username_1
username_1.send_keys("username")
# now, send the password element in it
password_1 = driver.find_element_by_id("login-password")
# then, send the keys for password_1
password_1.send_keys("password")
# then, get the tag for submit button
driver.find_element_by_id("login-submit").click()
find_elemet_by_id用于查找HTML标签”login-email”和”login-password”,然后我们将向它们发送按键。
现在,我们将设置网络部分:
代码:
def go_to_network():
driver.find_element_by_id("my_network-tab-icon").click()
现在,我们知道LinkedIn将尽力防止爬取。因此,我们很难找到连接按钮。我们可以使用类似Xpath的技术。
代码:
def send_requests_1():
# the Nnumber of requests we want to send
n_1 = input("The number of connection requests: ")
for k in range(0, n_1):
# position(in px) of connection button will be different for different user
pyt.click(810, 670)
print("Request send!")
这是我们如何在LinkedIn上使用Python的Web驱动程序发送自动连接请求。
代码的完整实现:
from selenium import webdriver as wd
from selenium.webdriver.common.keys import Keys as ky
import pyautogui as pyt
def login_1():
# First, we will get the login element
username_1 = driver.find_element_by_id("login-email")
# then, we will send the keys for username_1
username_1.send_keys("username")
# now, send the password element in it
password_1 = driver.find_element_by_id("login-password")
# then, send the keys for password_1
password_1.send_keys("password")
# then, get the tag for submit button
driver.find_element_by_id("login-submit").click()
def go_to_network():
driver.find_element_by_id("my_network-tab-icon").click()
def send_requests_1():
# the Nnumber of requests we want to send
n_1 = input("The number of connection requests: ")
for k in range(0, n_1):
# position(in px) of connection button will be different for different user
pyt.click(810, 670)
print("All request are sent!")
def main():
# url of LinkedIn,com
url_1 = "http://linkedin.com/"
# url of network page of linkedin.com
network_url1 = "http:/linkedin.com/my_network/"
# path for browser web driver
driver_1 = wd.Chrome('C:\Program Files\Web Driver\chromedriver.exe'')
driver_1.get(url_1)
# Drivers code
if __name__ == __main__:
main()
输出:
The number of connection requests: 12
All requests are sent!
结论
在本教程中,我们讨论了如何使用Python的Web驱动程序生成一个自动化系统,用于从LinkedIn发送连接请求。