-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_crawler_gen.py
More file actions
58 lines (41 loc) · 1.61 KB
/
Copy pathtest_crawler_gen.py
File metadata and controls
58 lines (41 loc) · 1.61 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import time
import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin
# define a class with variables for the single elements
class CrawledArticle():
def __init__(self, title, emoji, content, image):
self.title = title
self.emoji = emoji
self.content = content
self.image = image
# define a class to fetch the page, parse the content, assign it to the variables and put it in a list
class ArticleFetcher():
def fetch(self):
url = "http://python.beispiel.programmierenlernen.io/index.php"
while url != " ":
print(url)
time.sleep(1)
r = requests.get(url)
doc = BeautifulSoup(r.text, "html.parser")
for card in doc.select(".card"):
emoji = card.select_one(".emoji").text
content = card.select_one(".card-text").text
title = card.select(".card-title span")[1].text
image = urljoin(url, card.select_one("img").attrs["src"])
yield CrawledArticle(title, emoji, content, image)
button = doc.select_one(".navigation .btn")
# as long as there's a next page button, pass the url in the loop
if button:
next_page = urljoin(url, button.attrs["href"])
url = next_page
else:
url = " "
return articles
fetcher = ArticleFetcher()
counter = 0
for element in fetcher.fetch():
if counter == 9:
break
counter += 1
print(element.emoji + " " + element.title)