-
Notifications
You must be signed in to change notification settings - Fork 4
/
This Blog has X Views.py
165 lines (114 loc) · 7.48 KB
/
This Blog has X Views.py
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
163
164
165
##################################### -PLS READ- ################################################################################################
'''
The code you see below is from my article "This Blog has X Views and Y Claps".
The code will login into my Medium account, navigate Medium until it finds this article,
and then it will change the title to the amount of views and claps the article actually has.
This was all in an attempt to create a fully automated up-to-date article.
If you want to learn the code below and how to do cool projects like this one for yourself,
you can take my course in Web Scraping where by the end, you will understand what every line
below does.
Here's the link to it: https://www.udemy.com/course/web-scraping-in-python-with-beautifulsoup-and-selenium/?referralCode=939EB64B8E029FCBBDEB
The course is only $15, if you go to the link and it says anything above $100, that's just Udemy's
business model, just wait like 2 days and that price will reduce to around $10-$15.
'''
##################################### -Importing Necessary Libraries- ################################################################################################
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from bs4 import BeautifulSoup
import pandas as pd
import numpy as np
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
##################################### -Part 1: Logging into Medium- ################################################################################################
#--Summary: This section of code will login on my account for Medium, by inputting my email address and password
#Opens up a new browser and goes to the login page of Medium
driver = webdriver.Chrome('C:/Web Scraping course/chromedriver.exe')
driver.get('https://medium.com/m/signin')
#Will click on the sign in with Facebook button
driver.find_element_by_xpath('//*[@id="susi-modal-fb-button"]/div/a').click()
#The chunk of code below will input my email address and password then press the log in button
email = driver.find_element_by_xpath('//*[@id="email"]')
email.send_keys('######')
password = driver.find_element_by_xpath('//*[@id="pass"]')
password.send_keys('#######')
driver.find_element_by_xpath('//*[@id="loginbutton"]').click()
#This code tells the program to wait until the home page of Medium has loaded in
element = WebDriverWait(driver, 30).until(
EC.presence_of_element_located((By.XPATH, '//*[@id="root"]/div/nav/div/div/div/div/div[2]/div/div[5]/div/button/div/img')))
##################################### -Part 2: Navigating Medium- ################################################################################################
#--Summary: This section of code will navigate through medium to find the "This Blog has X Views and Y Claps" artice
#This chunk of code clicks on my profile and goes to my stats page
driver.find_element_by_xpath('//*[@id="root"]/div/nav/div/div/div/div/div[2]/div/div[5]/div/button/div/img').click()
time.sleep(1)
driver.find_element_by_xpath('//*[@id="metabarActionsMenu"]/div/ul/li[5]/p/a').click()
time.sleep(2)
driver.execute_script('window.scrollTo(0, document.body.scrollHeight)')
#This chunk of code goes through all my articles and finds the one with "Blog" in the title, saves the number of views it has and then goes to that stories page
soup = BeautifulSoup(driver.page_source, 'lxml')
posts = soup.find_all('tr', class_ = 'sortableTable-row js-statsTableRow')
df = pd.DataFrame({'Link':[''],'Name':[''], 'Views':['']})
for i in posts:
link = i.find('a').get('href')
name = i.find('a').text
views = i.find('span', class_ = 'sortableTable-number').get('title')
df = df.append({'Link':link,'Name':name, 'Views':views}, ignore_index = True)
def title(x):
if 'Blog' in x:
return 1
else:
return 0
df['Num'] = df['Name'].apply(title)
df2 = df[df['Num'] == 1]
views = df2.iloc[0,2]
driver.get(df2.iloc[0,0])
driver.find_element_by_xpath('//*[@id="root"]/div/div[4]/div[1]/div/div[1]/div[1]/div/div[2]/h2/a').click()
time.sleep(10)
##################################### -Part 3: Inside The Article- ################################################################################################
#--Summary: This section of code will perform all the changes inside the article and publish the newly edited story
#Finds and stores the number of claps the article has
num_claps = driver.find_element_by_xpath('//*[@id="root"]/div/div[3]/div[5]/div/div[1]/div/div[4]/div/div[1]/div[1]/span[2]/div/div[2]/div/p/button').text
num_claps = num_claps.split(' ')[0]
#Gets the username of the last person who clapped on the article
driver.find_element_by_xpath('//*[@id="root"]/div/div[3]/div[5]/div/div[1]/div/div[4]/div/div[1]/div[1]/span[2]/div/div[2]/div/p/button').click()
time.sleep(1)
last_person_clapped = driver.find_element_by_xpath('/html/body/div[2]/div/div[1]/div/div[2]/div/div[1]/div[2]/a/h2').text
driver.find_element_by_xpath('/html/body/div[2]/div/div[2]/div/div/button').click()
time.sleep(1)
#Clicks the edit story button
driver.find_element_by_xpath('/html/body/div/div/div[3]/article/div/section[1]/div[1]/div/div[2]/div/div/div[2]/div/div[3]/div/div/div/button').click()
driver.find_element_by_xpath('/html/body/div[2]/div/div/div/div[1]/ul/li[3]/p/a').click()
time.sleep(3)
#Deletes the inital title
driver.find_element_by_xpath('/html/body/div[1]/div[2]/div/main/article/div[1]/section[1]/div[2]/div[1]/h3').clear()
time.sleep(1)
#Puts the new title in place with the number of views and claps in it
new_title = driver.find_element_by_xpath('/html/body/div[1]/div[2]/div/main/article/div[1]/section[1]/div[2]/div[1]/h3')
new_title.send_keys('This Blog Has ' +views+ ' Views and ' +num_claps+ ' Claps')
time.sleep(2)
#Changes the display name so people see can see the change
driver.find_element_by_xpath('/html/body/div[1]/div[2]/div/div[2]/div[2]/div[2]/div[3]/button').click()
time.sleep(1)
driver.find_element_by_xpath('/html/body/div[1]/div[2]/div/div[2]/div[3]/div[1]/ul/li[8]/button').click()
time.sleep(1)
driver.find_element_by_xpath('/html/body/div[1]/div[2]/div/div[2]/div[3]/div[1]/div/div/div[1]/div[2]/p').clear()
time.sleep(2)
new_title = driver.find_element_by_xpath('/html/body/div[1]/div[2]/div/div[2]/div[3]/div[1]/div/div/div[1]/div[2]')
new_title.send_keys('This Blog Has ' +views+ ' Views and ' +num_claps+ ' Claps')
time.sleep(1)
driver.find_element_by_xpath('/html/body/div[1]/div[2]/div/div[2]/div[3]/div[1]/div/div/div[4]/div/button').click()
time.sleep(1)
driver.find_element_by_xpath('/html/body/div[1]/div[2]/div/div[2]/div[2]/div[2]/div[3]/button').click()
time.sleep(1)
#Goes to the "last person who clapped was" line and inputs the username that the code saved
driver.find_element_by_xpath('/html/body/div[1]/div[2]/div/main/article/div[1]/section[2]/div[2]/div/h3[4]').clear()
time.sleep(1)
last_clapped = driver.find_element_by_xpath('/html/body/div[1]/div[2]/div/main/article/div[1]/section[2]/div[2]/div/h3[4]')
last_clapped.send_keys('The last person who clapped was ' + str(last_person_clapped))
last_clapped.send_keys(Keys.ENTER)
time.sleep(2)
#Saves and publishes the article
driver.find_element_by_xpath('/html/body/div[1]/div[2]/div/div[2]/div[2]/div[2]/div[2]/button').click()
time.sleep(3)
driver.close()