forked from lvova78/crawler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrawler.py
More file actions
43 lines (31 loc) · 1.25 KB
/
Copy pathcrawler.py
File metadata and controls
43 lines (31 loc) · 1.25 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
import requests
from bs4 import BeautifulSoup
def crawl():
headers = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 '
'(KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36'
}
games = []
page_number = 1
while True:
page_url = f'https://marketplace.xbox.com/en-US/Games/Xbox360Games?Page={page_number}'
print(f"Loading page #{page_number}: {page_url}")
response = requests.get(page_url,headers=headers)
print("Done")
soup = BeautifulSoup(response.content, 'html.parser')
if soup.find_all("div", {"class": "NoGamesFound"}):
# if we reached the end, there is a div with class 'NoGamesFound',
# so we stop crawling
print("No data on page, finishing execution.")
break
for item in soup.findAll('li', class_='grid-6'):
rating_str = item.find('div', class_='UserRatingStarStrip').get_text(strip=True)
game = dict(
title=item.find('h2').get_text(strip=True),
rating=float(rating_str.split(' ')[0])
)
games.append(game)
print("Parsed game:", game)
page_number += 1
if __name__ == '__main__':
crawl()