|
1 | 1 | #!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
2 | 4 | """ |
3 | 5 | Show off how to scrape information from a webpage. |
4 | 6 | """ |
|
15 | 17 |
|
16 | 18 |
|
17 | 19 | # Get webpage |
18 | | -url = "http://cc1.dbwebb.se/" |
| 20 | +url = "https://dbwebb.se/irclog/" |
19 | 21 | print("\nReady to send HTTP request to ", url, "\nPress enter to continue. ", end='') |
20 | 22 | input() |
21 | 23 | req = requests.get(url) |
|
24 | 26 |
|
25 | 27 |
|
26 | 28 | # Get the webpage content as a soup |
27 | | -soup = BeautifulSoup(req.text) |
28 | | - |
29 | | -ircLog = soup.find("p", class_="irclog") |
30 | | -print("\nThe scraped content (the irclog) is:\n", ircLog) |
31 | | - |
32 | | -entries = ircLog("span") |
| 29 | +soup = BeautifulSoup(req.text, "html5lib") |
33 | 30 |
|
34 | | -str1 = "{time} <{user}> {message}" |
| 31 | +# Find the intresting parts in the soup |
| 32 | +ircLog = soup.find("tbody") |
| 33 | +logText = [element.text for element in ircLog.tr.findAll("td")] |
35 | 34 |
|
36 | | -print("\nPretty print log details:") |
37 | | -print(str1.format(time=entries[0].text, user=entries[1].text, message=entries[2].text)) |
38 | | -print(str1.format(time=entries[3].text, user=entries[4].text, message=entries[5].text)) |
39 | | -print(str1.format(time=entries[6].text, user=entries[7].text, message=entries[8].text)) |
| 35 | +# Print out the content |
| 36 | +formatString = "{} <{}> {}" |
| 37 | +print("\nLast entry in IRC-log:") |
| 38 | +print(formatString.format(*logText)) |
0 commit comments