This repository has been archived on 2024-12-13. You can view files and clone it, but cannot push or open issues or pull requests.
helseveileder_webscraper/main.py
Sindre Kjelsrud 0da7504104
spørsmål is scraped!
Co-authored-by: haraldnilsen <harald_998@hotmail.com>
Signed-off-by: Sindre Kjelsrud <kjelsrudsindre@gmail.com>
2024-01-08 14:14:05 +01:00

60 lines
No EOL
1.7 KiB
Python

import httpx
from selectolax.parser import HTMLParser
def fetch_question_url(url):
# Define headers
headers = {
"User-Agent":"Mozilla/5.0 (X11; Linux x86_64; rv:121.0) Gecko/20100101 Firefox/121.0"
}
# Define the parameters for the request
params = {
"action": "get-faqs",
"idSource": "2",
"list": "frequently_asked_questions",
"idCategory": "8288", # Id for category
"from": "0",
"size": "10", # Adjust this to get more results
"gender": "false",
"age": "false",
"zone": "default",
"filter_query": "",
"skip": "0",
"load_categories": "false"
}
response = httpx.get(url, params=params, headers=headers)
# Check if the request was successful
if response.status_code == 200:
# Parse the response
data = response.json()
# Extracting URLs from each item
urls = [item['url'] for item in data['items']]
return urls
else:
print(f"Failed to fetch data: {response.status_code}")
def fetch_all_info(url):
# Define headers
headers = {
"User-Agent":"Mozilla/5.0 (X11; Linux x86_64; rv:121.0) Gecko/20100101 Firefox/121.0"
}
response = httpx.get(url, headers=headers)
html = HTMLParser(response.text)
# Extracting the question and answer
sporsmal = html.css_first('div.article-text').text()
# Create array of results
#result = [sporsmal, svar, signatur]
return sporsmal
studenterspor_url = "https://www.studenterspor.no/ajax_handler.php"
urls = fetch_question_url(studenterspor_url)
for url in urls:
print(fetch_all_info(url))