-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patht_8_htmlDraw.py
More file actions
50 lines (40 loc) · 1.58 KB
/
Copy patht_8_htmlDraw.py
File metadata and controls
50 lines (40 loc) · 1.58 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
41
42
43
44
45
46
47
48
49
50
from bs4 import BeautifulSoup
import requests
import time
def fetch_html(url):
"""获取指定url的html内容"""
headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36"
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
return response.text
else:
print(f"获取网页内容失败,状态码:{response.status_code}")
return None
def extract_text_from_html(html_content):
"""从HTML内容中提取正文文本"""
soup = BeautifulSoup(html_content, 'html.parser')
# 去掉不必要的脚本和样式内容
for script in soup(["script", "style"]):
script.extract()
# 获取正文文本
text = soup.get_text(separator='\n', strip=True)
return text
def extract_link_from_html(html_content):
"""从HTML内容中提取链接信息"""
soup = BeautifulSoup(html_content, 'html.parser')
links = set()
for tag, attr in [('a', 'href'), ('img', 'src'), ('link', 'href'), ('script', 'src'), ('iframe', 'href')]:
for element in soup.find_all(tag, **{attr: True}):
if element[attr].startswith('http'):
links.add(element[attr])
return sorted(links)
if __name__ == "__main__":
url = "https://www.google.com/" # 替换为你要抓取的网页URL
html_content = fetch_html(url)
if html_content:
links = extract_link_from_html(html_content)
print("提取到的链接:")
for link in links:
print(link)