Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
level3-lesson24-diy2
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
b66928ca
authored
Nov 24, 2024
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
1c74bdbe
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
60 additions
and
2 deletions
m.py
my_analysis.py
n.py
m.py
0 → 100644
View file @
b66928ca
import
re
import
requests
url
=
'https://v.qq.com/'
# 替换为你想要爬取的网页链接
response
=
requests
.
get
(
url
)
html_content
=
response
.
text
# 使用正则表达式提取网页标题
title_pattern
=
re
.
compile
(
r'<title>(.*?)</title>'
)
title
=
re
.
search
(
title_pattern
,
html_content
)
if
title
:
print
(
title
.
group
(
1
))
# 使用正则表达式提取所有段落内容
paragraph_pattern
=
re
.
compile
(
r'<p>(.*?)</p>'
)
paragraphs
=
re
.
findall
(
paragraph_pattern
,
html_content
)
for
paragraph
in
paragraphs
:
print
(
paragraph
)
\ No newline at end of file
my_analysis.py
View file @
b66928ca
...
...
@@ -2,4 +2,7 @@ from pyecharts.charts import Bar
subjects
=
[
"浈浈"
,
"聪聪"
,
"小智"
,
"波奇"
]
scores1
=
[
92
,
95
,
82
,
88
]
# 期中成绩
scores2
=
[
95
,
79
,
93
,
90
]
# 期末成绩
\ No newline at end of file
scores2
=
[
95
,
79
,
93
,
90
]
# 期末成绩
bar
=
Bar
()
bar
.
add_xaxis
(
subjects
)
bar
.
add_yaxis
()
\ No newline at end of file
n.py
0 → 100644
View file @
b66928ca
import
requests
from
bs4
import
BeautifulSoup
def
fetch_webpage
(
url
):
try
:
response
=
requests
.
get
(
url
)
response
.
raise_for_status
()
# 如果请求失败(如404, 500等),将抛出HTTPError异常
return
response
.
text
except
requests
.
exceptions
.
RequestException
as
e
:
print
(
f
"Error fetching the webpage: {e}"
)
return
None
def
parse_html
(
html_content
):
soup
=
BeautifulSoup
(
html_content
,
'html.parser'
)
headers
=
[]
# 提取所有h1, h2, h3, h4, h5, h6标签
for
header_tag
in
[
'h1'
,
'h2'
,
'h3'
,
'h4'
,
'h5'
,
'h6'
]:
headers
.
extend
(
soup
.
find_all
(
header_tag
))
return
headers
def
print_headers
(
headers
):
for
header
in
headers
:
print
(
f
"{header.name}: {header.get_text(strip=True)}"
)
def
main
():
url
=
'https://v.qq.com/'
# 替换为你要爬取的网页URL
html_content
=
fetch_webpage
(
url
)
if
html_content
:
headers
=
parse_html
(
html_content
)
print_headers
(
headers
)
if
__name__
==
"__main__"
:
main
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment