-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathscraper.py
49 lines (39 loc) · 1.43 KB
/
scraper.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
from bs4 import BeautifulSoup
import requests
def scrape(url):
response = requests.get(url)
if response.status_code == 200:
return BeautifulSoup(response.content, 'html.parser')
else:
response.raise_for_status()
def scrape_table(url):
response = requests.get(url)
if response.status_code == 200:
soup = BeautifulSoup(response.content, 'html.parser')
tables = soup.find_all('table')
result = []
for table in tables:
headers = [th.get_text().strip() for th in table.find_all('th')]
rows = table.find_all('tr')
table_data = []
for row in rows:
cells = row.find_all('td')
if cells:
data = {headers[i]: cell.get_text().strip() for i, cell in enumerate(cells)}
table_data.append(data)
result.append(table_data)
return result
else:
response.raise_for_status()
def scrape_images(url):
soup = scrape(url)
images = [{'alt': img.get('alt', ''), 'src': img.get('src', '')} for img in soup.find_all('img')]
return images
def extract_links(url):
soup = scrape(url)
links = [{'text': a.text, 'href': a.get('href', '')} for a in soup.find_all('a')]
return links
def extract_metadata(url):
soup = scrape(url)
metadata = {meta.get('name', ''): meta.get('content', '') for meta in soup.find_all('meta')}
return metadata