|
| 1 | +import re |
| 2 | + |
1 | 3 | from bs4 import BeautifulSoup
|
2 | 4 |
|
3 | 5 | from uk_bin_collection.uk_bin_collection.common import *
|
@@ -91,40 +93,56 @@ def parse_data(self, page: str, **kwargs) -> dict:
|
91 | 93 |
|
92 | 94 | soup = BeautifulSoup(response.text, features="html.parser")
|
93 | 95 | soup.prettify()
|
94 |
| - |
| 96 | + # print(soup) |
95 | 97 | # Find all the bits of the current calendar that contain an event
|
96 |
| - events = soup.find_all("div", {"class": "rc-event-container"}) |
| 98 | + resultscontainer = soup.find_all("div", {"class": "results-container"}) |
97 | 99 |
|
98 |
| - for event in events: |
99 |
| - # Get the date and type of each bin collection |
100 |
| - bin_date = datetime.strptime( |
101 |
| - event.find_next("a").attrs.get("data-original-datetext"), |
102 |
| - "%A %d %B, %Y", |
| 100 | + for result in resultscontainer: |
| 101 | + rows = result.find_all( |
| 102 | + "div", {"class": "col-12 col-sm-6 col-md-4 col-lg-4 mb-4"} |
103 | 103 | )
|
104 |
| - bin_type = event.find_next("a").attrs.get("data-original-title") |
105 |
| - # Only process it if it's today or in the future |
106 |
| - if bin_date.date() >= datetime.now().date(): |
107 |
| - # Split the really long type up into two separate bins |
108 |
| - if ( |
109 |
| - bin_type |
110 |
| - == "Mixed dry recycling (blue lidded bin) and glass (black box or basket)" |
111 |
| - ): |
112 |
| - collections.append( |
113 |
| - ( |
114 |
| - "Mixed dry recycling (blue lidded bin)", |
115 |
| - datetime.strftime(bin_date, date_format), |
| 104 | + for row in rows: |
| 105 | + cardcollectionday = row.find( |
| 106 | + "span", {"class": "card-collection-day"} |
| 107 | + ) |
| 108 | + cardcollectiondate = row.find( |
| 109 | + "span", {"class": "card-collection-date"} |
| 110 | + ) |
| 111 | + cardcollectionmonth = row.find( |
| 112 | + "span", {"class": "card-collection-month"} |
| 113 | + ) |
| 114 | + bin_type = row.find( |
| 115 | + "li", {"class": re.compile(r"collection-type-...$")} |
| 116 | + ).text |
| 117 | + |
| 118 | + collection_date = f"{cardcollectionday.text}{cardcollectiondate.text}{cardcollectionmonth.text}" |
| 119 | + bin_date = datetime.strptime( |
| 120 | + collection_date, |
| 121 | + "%A %d %B %Y", |
| 122 | + ) |
| 123 | + |
| 124 | + if bin_date.date() >= datetime.now().date(): |
| 125 | + # Split the really long type up into two separate bins |
| 126 | + if ( |
| 127 | + bin_type |
| 128 | + == "Mixed dry recycling (blue lidded bin) and glass (black box or basket)" |
| 129 | + ): |
| 130 | + collections.append( |
| 131 | + ( |
| 132 | + "Mixed dry recycling (blue lidded bin)", |
| 133 | + datetime.strftime(bin_date, date_format), |
| 134 | + ) |
| 135 | + ) |
| 136 | + collections.append( |
| 137 | + ( |
| 138 | + "Glass (black box or basket)", |
| 139 | + datetime.strftime(bin_date, date_format), |
| 140 | + ) |
116 | 141 | )
|
117 |
| - ) |
118 |
| - collections.append( |
119 |
| - ( |
120 |
| - "Glass (black box or basket)", |
121 |
| - datetime.strftime(bin_date, date_format), |
| 142 | + else: |
| 143 | + collections.append( |
| 144 | + (bin_type, datetime.strftime(bin_date, date_format)) |
122 | 145 | )
|
123 |
| - ) |
124 |
| - else: |
125 |
| - collections.append( |
126 |
| - (bin_type, datetime.strftime(bin_date, date_format)) |
127 |
| - ) |
128 | 146 |
|
129 | 147 | data = {"bins": []}
|
130 | 148 |
|
|
0 commit comments