-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrape.py
28 lines (20 loc) · 823 Bytes
/
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
# -*- coding: utf-8 -*-
from lxml import html
import requests
import sys
import json
amount = int(raw_input("How many of the top movies do you want to get?\n"))
if amount % 50 != 0:
amount = int(raw_input("Please type a number in steps of 50\n"))
pages = amount / 50
base_url = "https://www.imdb.com/search/title?groups=top_1000&sort=user_rating&view=simple&page="
all_titles = []
for idx in range(pages):
url = "{}{}".format(base_url, idx)
page = requests.get(url, headers={'Accept-Language': 'en-EN'}) # change your language here
tree = html.fromstring(page.content)
titles_arr = tree.xpath("//span[@class='lister-item-header']/span/a/text()")
all_titles += titles_arr
print("Scraping pages {}".format(idx))
with open("filme.json", 'wb') as outfile:
json.dump(all_titles, outfile)