-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetchurl.py
85 lines (64 loc) · 2.48 KB
/
fetchurl.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import requests
from bs4 import BeautifulSoup
from typing import Optional
def get_content_from_url(url: str, content_type: str = 'html', max_length: Optional[int] = None) -> str:
"""
Fetch and return the content from a given URL.
Returns:
str: The content of the webpage, or an error message.
"""
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.35"
}
try:
response = requests.get(url, headers=headers, timeout=10)
response.raise_for_status() # Raises an HTTPError for bad responses
soup = BeautifulSoup(response.text, 'html.parser')
if content_type == 'html':
content = str(soup)
elif content_type == 'text':
content = soup.get_text(strip=True)
else:
raise ValueError("Invalid content_type. Use 'html' or 'text'.")
if max_length is not None:
content = content[:max_length]
return content
except requests.RequestException as e:
error_msg = f"Failed to retrieve content from {url}. Error: {str(e)}"
print(error_msg)
return error_msg
except ValueError as e:
print(str(e))
return str(e)
def extract_image_urls(url):
"""
Extracts all image URLs from the given HTML content.
Returns:
list: A list of absolute URLs of all images found in the HTML.
"""
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.35"
}
try:
response = requests.get(url, headers=headers, timeout=10)
response.raise_for_status() # Raises an HTTPError for bad responses
except requests.RequestException as e:
error_msg = f"Failed to retrieve content from {url}. Error: {str(e)}"
print(error_msg)
return error_msg
except ValueError as e:
print(str(e))
return str(e)
soup = BeautifulSoup(response.text, 'html.parser')
img_tags = soup.find_all('img')
urls = []
for img in img_tags:
# Get the 'src' attribute of the img tag
src = img.get('src')
if src:
# Make the URL absolute by joining it with the base URL
absolute_url = src #urljoin(base_url, src)
urls.append(absolute_url)
return urls