forked from deepeshguru/scraping-from-social-site
-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter_scraping.py
148 lines (98 loc) · 3.63 KB
/
twitter_scraping.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
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 4 15:01:12 2019
@author: tvs13
"""
from bs4 import BeautifulSoup
from selenium import webdriver
import time
import pandas as pd
post_want = 50
source = 'https://twitter.com/katyperry'
driver = webdriver.Chrome(executable_path=r"C:\Users\tvs13\Downloads\chromedriver.exe")
driver.get(source)
driver.maximize_window()
html = driver.page_source
soup = BeautifulSoup(html, 'lxml')
ul = soup.find("ul", class_="ProfileNav-list")
lis = ul.find_all("li")
tweets = lis[0].text.split()[-1]
following = lis[1].text.split()[-1]
followers = lis[2].text.split()[-1]
likes = lis[3].text.split()[-1]
name = soup.find("a", class_="ProfileHeaderCard-nameLink u-textInheritColor js-nav").text
ol = soup.find("ol", class_="stream-items js-navigable-stream")
data = []
try:
pinned = ol.find("li", class_="js-stream-item stream-item stream-item js-pinned ")
pinned_tweet = list(set(ol.find("li", class_="js-stream-item stream-item stream-item js-pinned ").text.split('\n')))
temp = [0] * 4
temp = [0] * 4
temp[0] = pinned.find("p", class_="TweetTextSize TweetTextSize--normal js-tweet-text tweet-text").text
for j in pinned_tweet:
if 'likes' in j:
temp[3] = j
elif 'retweets' in j:
temp[2] = j
elif 'replies' in j:
temp[1] = j
data.append(temp)
except:
pass
lis = soup.find_all("li", class_="js-stream-item stream-item stream-item ")
for k in lis:
temp = [0] * 4
temp[0] = k.find("p", class_="TweetTextSize TweetTextSize--normal js-tweet-text tweet-text").text
i = k.text
i = i.split("\n")
i = list(set(i))
for j in i:
if 'likes' in j:
temp[3] = j
elif 'retweets' in j:
temp[2] = j
elif 'replies' in j:
temp[1] = j
data.append(temp)
SCROLL_PAUSE_TIME = 10
# Get scroll height
last_height = driver.execute_script("return document.body.scrollHeight")
new_data = []
while(1): # Scroll down to bottom
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
# Wait to load page
time.sleep(SCROLL_PAUSE_TIME)
html = driver.page_source
soup = BeautifulSoup(html, 'lxml')
lis = soup.find_all("li", class_="js-stream-item stream-item stream-item ")
for k in lis:
temp = [0] * 4
temp[0] = k.find("p", class_="TweetTextSize TweetTextSize--normal js-tweet-text tweet-text").text
i = k.text
i = i.split("\n")
i = list(set(i))
for j in i:
if 'likes' in j:
temp[3] = j
elif 'retweets' in j:
temp[2] = j
elif 'replies' in j:
temp[1] = j
data.append(temp)
# Calculate new scroll height and compare with last scroll height
new_height = driver.execute_script("return document.body.scrollHeight")
df = pd.DataFrame(data)
df = df.drop_duplicates()
if len(df) >= post_want:
break
if new_height == last_height:
break
last_height = new_height
data = {"Name": name, "Total tweets": tweets, "Following": following, "Followers": followers, "Likes": likes}
data["post infos"]={}
for i in range(min(len(df), post_want)):
data["post infos"][str(i+1)] = {"post": df.iloc[i, 0], "Reply": df.iloc[i, 1],
"Retweet": df.iloc[i, 2], "Like": df.iloc[i, 3]}
import json
with open(name+ '_' + 'twitter.json', 'w') as fp:
json.dump(data, fp)