-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlearning-python-3.py
More file actions
32 lines (27 loc) · 911 Bytes
/
learning-python-3.py
File metadata and controls
32 lines (27 loc) · 911 Bytes
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
"""
Utilisation de :
- csv
- writer
- writerow
"""
import csv
import requests
from bs4 import BeautifulSoup
url = "https://www.gov.uk/search/news-and-communications"
response = requests.get(url)
page = BeautifulSoup(response.content, "html.parser")
def getPageTagsContent(tag, className):
tagsStringed = []
tags = page.find_all(tag, className)
for tag in tags:
tagsStringed.append(tag.string)
return tagsStringed
titles = getPageTagsContent("a", "gem-c-document-list__item-title")
descriptions = getPageTagsContent("p", "gem-c-document-list__item-description")
header = ["title", "description"]
with open("titles_and_descriptions.csv", "w") as csv_file:
writer = csv.writer(csv_file, delimiter=";")
writer.writerow(header)
for title, description in zip(titles, descriptions):
writer.writerow([title, description])
print("File saved in your local repository")