Print select details from a html response containing xml @param xml_news_url: url to parse
(xml_news_url, counter)
| 5 | |
| 6 | |
| 7 | def news(xml_news_url, counter): |
| 8 | """Print select details from a html response containing xml |
| 9 | @param xml_news_url: url to parse |
| 10 | """ |
| 11 | |
| 12 | context = ssl._create_unverified_context() |
| 13 | Client = urlopen(xml_news_url, context=context) |
| 14 | xml_page = Client.read() |
| 15 | Client.close() |
| 16 | |
| 17 | soup_page = soup(xml_page, "xml") |
| 18 | |
| 19 | news_list = soup_page.findAll("item") |
| 20 | i = 0 # counter to print n number of news items |
| 21 | |
| 22 | for news in news_list: |
| 23 | print(f"news title: {news.title.text}") # to print title of the news |
| 24 | print(f"news link: {news.link.text}") # to print link of the news |
| 25 | print(f"news pubDate: {news.pubDate.text}") # to print published date |
| 26 | print("+-" * 20, "\n\n") |
| 27 | |
| 28 | if i == counter: |
| 29 | break |
| 30 | i = i + 1 |
| 31 | |
| 32 | |
| 33 | # you can add google news 'xml' URL here for any country/category |