-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript_first_version.py
More file actions
162 lines (115 loc) · 5.43 KB
/
script_first_version.py
File metadata and controls
162 lines (115 loc) · 5.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from parsel import Selector
from time import sleep
from data import persons
import json, random, os
from pymongo.mongo_client import MongoClient
from selenium.webdriver.chrome.options import Options
base_dir = os.getcwd()
# logout = 'https://www.linkedin.com/uas/login/m/logout'
# configure web browser
chrome_option = Options()
chrome_option.add_argument('--proxy-server=223.205.99.64:8080')
# chrome_option.add_argument('headless=True')
# chrome_option.add_argument('user-agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"')
""" შევქმნათ ვებბრაუზერის ობიექტი და დავუკავშირდეთ ჩვენთვის საჭირო ლინკს """
driver = webdriver.Chrome(options=chrome_option)
driver.get('https://www.linkedin.com/uas/login')
# driver.delete_all_cookies()
def login(username, password):
""" აუთენთიფიკაციის ფუნქცია """
user = driver.find_element_by_name('session_key').send_keys(username)
sleep(200)
password = driver.find_element_by_name('session_password').send_keys(password)
sleep(10)
log_in_button = driver.find_element_by_class_name('btn__primary--large').click()
print('you are logged in successfully')
""" რენდომად ამოვირჩიოთ ყალბი მომხმარებელი json ფაილში შენახული ექაუნთებიდან """
with open('fake_credentials.json') as file:
accounts = json.load(file)
user = random.choice(accounts['accounts'])
# random.choice(credentials)
username = user['username']
password = user['password']
try:
login(username, password)
except:
print("can't login now, try another credentials")
print(driver.current_url)
""" data ფაილისგან შედგენილი ქვაერების მიხედვით წამოვიღოთ პროფილის ინფორმაციები: """
queries = [person["person_name"] + " " + person['title'] + " " + person['city'] for person in persons]
print(queries)
profile_urls = []
for query in queries:
try:
# მოვნიშნოთ შიდა ძებნის html ელემენტი
search = driver.find_element_by_css_selector('#ember33 > input[type=text]')
# გადავცეთ შესაბამისი ქვაერი
search.send_keys(query)
# გავუშვათ ძებნა
search.send_keys(Keys.ENTER)
sleep(3)
# შიდა ძებნის შედეგი
result = driver.find_element_by_class_name('EntityPhoto-circle-4-ghost-person')
# გადავიდეთ პროფილის გვერდზე
result.click()
sleep(5)
print(driver.current_url)
profile_urls.append(driver.current_url)
except:
# driver.back()
# გავასუფთავოთ ძებნის ელემენტი წარუმატებელი ქვაერისგან
search.clear()
sleep(5)
print("add later!!!!")
sleep(3)
print(driver.current_url)
print ("profiles:", profile_urls)
final_results = {}
single_profile_dict={}
for url in profile_urls:
"""მოძებნილი პროფილებიდან დეტალური ინფორმაციის წამოღება"""
profile = driver.get(url)
print('URL: ', driver.current_url)
sleep(5)
# შევინახოთ პროფილის html სორსი ცალკე ცვლადში
profile_selector = Selector(text=driver.page_source)
print(profile_selector)
sleep(random.randint(2, 5))
# full name of user
person_name = profile_selector.xpath('// *[ @ id = "ember43"] / div[2] / div[2] / div[1] / ul[1] / li[1]/text()').get()
if person_name:
person_name = person_name.strip()
print("Person_name", person_name)
# working experience (Where he/she worked)
experience = profile_selector.xpath("// div[2] / h3/ text()").getall()
print("Experiense: ", experience)
# current position or job title (CTO, CEO, Senior Engineer, etc)
position = profile_selector.xpath('//*[@id="ember43"]/div[2]/div[2]/div[1]/h2/text()').get()
if position:
position = position.strip()
print("Position: ", position)
# list of companies where he/she worked at
companies = profile_selector.xpath("// div[2] / h4 / span[2]/ text()").getall()
print("Companies: ", companies)
single_profile_dict = {}
single_profile_dict['_id'] = url
single_profile_dict['person_name'] = person_name
single_profile_dict['experience'] = experience
single_profile_dict['position'] = position
single_profile_dict['companies'] = companies
final_results[url] = single_profile_dict
driver.quit()
"""საბოლოო შედეგების ბაზაში შენახვა"""
#connect MongoDB local machine
client = MongoClient('localhost', 27017)
#db
db = client.linkedin
#collection
collection = db.user_info
# Insert
collection.insert_many(final_results.values())
print("final results:", final_results)