-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequestutils.py
More file actions
40 lines (30 loc) · 1.08 KB
/
requestutils.py
File metadata and controls
40 lines (30 loc) · 1.08 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
#!/usr/bin/python3
import requests
from fake_useragent import UserAgent
try:
from BeautifulSoup import BeautifulSoup
except ImportError:
from bs4 import BeautifulSoup
def form_url(day, legislatura, main_url):
fday = f"{day:%d}"
fmonth = f"{day:%m}"
return main_url.format(LEGISLATURA=legislatura, DD=fday, MM=fmonth, YY=day.year)
def get_download_link(url, scrap_url):
soup = get_soup(url)
results = soup.find("a", download=True)
if results is None:
return ""
return scrap_url.format(zip_url=results["href"])
def get_soup(url):
ua = UserAgent()
req = get_request_with_headers(url)
return BeautifulSoup(req.text, "lxml")
def get_request_with_headers(url: str):
session = requests.session()
s_result = session.get(url)
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36',
'referer': url,
'Cookie': str(requests.utils.dict_from_cookiejar(s_result.cookies))}
req = requests.get(url, headers=headers)
return req