-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathinsta_scrape.py
142 lines (120 loc) · 4.3 KB
/
insta_scrape.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
import time
import re
import urllib
from selenium.webdriver.firefox.options import Options
from selenium.webdriver import Firefox
def recent_post_links(username, post_count=10):
"""
With the input of an account page, scrape the 10 most recent posts urls
Args:
username: Instagram username
post_count: default of 10, set as many or as few as you want
Returns:
A list with the unique url links for the most recent posts for the provided user
"""
url = "https://www.instagram.com/" + username + "/"
firefox_options = Options()
firefox_options.add_argument("--headless")
browser = Firefox(firefox_options=firefox_options)
browser.get(url)
post = 'https://www.instagram.com/p/'
post_links = []
while len(post_links) < post_count:
links = [a.get_attribute('href')
for a in browser.find_elements_by_tag_name('a')]
for link in links:
if post in link and link not in post_links:
post_links.append(link)
scroll_down = "window.scrollTo(0, document.body.scrollHeight);"
browser.execute_script(scroll_down)
time.sleep(10)
else:
browser.stop_client()
return post_links[:post_count]
def find_hashtags(comment):
"""
Find hastags used in comment and return them
Args:
comment: Instagram comment text
Returns:
a list or individual hashtags if found in comment
"""
hashtags = re.findall('#[A-Za-z]+', comment)
if (len(hashtags) > 1) & (len(hashtags) != 1):
return hashtags
elif len(hashtags) == 1:
return hashtags[0]
else:
return ""
def find_mentions(comment):
"""
Find mentions used in comment and return them
Args:
comment: Instagram comment text
Returns:
a list or individual mentions if found in comment
"""
mentions = re.findall('@[A-Za-z]+', comment)
if (len(mentions) > 1) & (len(mentions) != 1):
return mentions
elif len(mentions) == 1:
return mentions[0]
else:
return ""
def insta_link_details(url):
"""
Take a post url and return post details
Args:
urls: a list of urls for Instagram posts
Returns:
A list of dictionaries with details for each Instagram post, including link,
post type, like/view count, age (when posted), and initial comment
"""
firefox_options = Options()
firefox_options.add_argument("--headless")
browser = Firefox(firefox_options=firefox_options)
browser.get(url)
try:
# This captures the standard like count.
likes = browser.find_element_by_xpath(
"""/html/body/div[1]/section/main/div/div/article/
div[3]/section[2]/div/div/button/span""").text.split()[0]
post_type = 'photo'
except:
# This captures the like count for videos which is stored
likes = browser.find_element_by_xpath(
"""/html/body/div[1]/section/main/div/div/article/
div[3]/section[2]/div/span/span""").text.split()[0]
post_type = 'video'
age = browser.find_element_by_css_selector('a time').text
comment = browser.find_element_by_xpath(
"""/html/body/div[1]/section/main/div/div[1]/article/
div[3]/div[1]/ul/div/li/div/div/div[2]/span""").text
hashtags = find_hashtags(comment)
mentions = find_mentions(comment)
post_details = {'link': url, 'type': post_type, 'likes/views': likes,
'age': age, 'comment': comment, 'hashtags': hashtags,
'mentions': mentions}
time.sleep(10)
return post_details
def insta_url_to_img(url, filename="insta.jpg"):
"""
Getting the actual photo file from an Instagram url
Args:
url: Instagram direct post url
filename: file name for image at url
Returns:
image file, saved locally
"""
firefox_options = Options()
firefox_options.add_argument("--headless")
browser = Firefox(firefox_options=firefox_options)
browser.get(url)
try:
image = browser.find_element_by_xpath(
"""/html/body/span/section/main/div/div/article/
div[1]/div/div/div[1]/div[1]/img""").get_attribute('src').split(' ')[0]
urllib.request.urlretrieve(image, filename)
# If image is not a photo, print notice
except:
print("No image")