BeatuifulSoup库的基本元素

BeatuifulSoup库的基本元素

>>> import requests
>>> r=requests.get("http://python123.io/ws/demo.html")
>>> r.text
'<html><head><title>This is a python demo page</title></head>\r\n<body>\r\n<p class="title"><b>The demo python introduces several python courses.</b></p>\r\n<p class="course">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\r\n<a href="http://www.icourse163.org/course/BIT-268001" class="py1" id="link1">Basic Python</a> and <a href="http://www.icourse163.org/course/BIT-1001870001" class="py2" id="link2">Advanced Python</a>.</p>\r\n</body></html>'
>>> demo=r.text
>>> demo
'<html><head><title>This is a python demo page</title></head>\r\n<body>\r\n<p class="title"><b>The demo python introduces several python courses.</b></p>\r\n<p class="course">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\r\n<a href="http://www.icourse163.org/course/BIT-268001" class="py1" id="link1">Basic Python</a> and <a href="http://www.icourse163.org/course/BIT-1001870001" class="py2" id="link2">Advanced Python</a>.</p>\r\n</body></html>'
>>> from bs4 import BeautifulSoup
>>> soup=BeautifulSoup(demo,"html.parser")
>>> print(soup.prettify())
<html>
 <head>
  <title>
   This is a python demo page
  </title>
 </head>
 <body>
  <p class="title">
   <b>
    The demo python introduces several python courses.
   </b>
  </p>
  <p class="course">
   Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:
   <a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">
    Basic Python
   </a>
   and
   <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">
    Advanced Python
   </a>
   .
  </p>
 </body>
</html>

在这里插入图片描述
在这里插入图片描述
或者import bs4
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

>>> from bs4 import BeautifulSoup
>>> import requests
>>> r=requests.get("http://python123.io/ws/demo.html")
>>> demo=r.text
>>> demo
'<html><head><title>This is a python demo page</title></head>\r\n<body>\r\n<p class="title"><b>The demo python introduces several python courses.</b></p>\r\n<p class="course">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\r\n<a href="http://www.icourse163.org/course/BIT-268001" class="py1" id="link1">Basic Python</a> and <a href="http://www.icourse163.org/course/BIT-1001870001" class="py2" id="link2">Advanced Python</a>.</p>\r\n</body></html>'
>>> soup=BeautifulSoup(demo,"html.parser")
>>> soup.title
<title>This is a python demo page</title>
>>> tag=soup.a
>>> tag
<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>
>>> soup.a.name
'a'
>>> soup.a.parent.name
'p'
>>> soup.a.parent.parent.name
'body'
>>> tag.attrs
{'href': 'http://www.icourse163.org/course/BIT-268001', 'class': ['py1'], 'id': 'link1'}
>>> tag.attrs['class']
['py1']
>>> tag.attrs['href']
'http://www.icourse163.org/course/BIT-268001'
>>> type(tag.attrs)
<class 'dict'>
>>> type(tag)
<class 'bs4.element.Tag'>
>>> soup.a
<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>
>>> soup.a.string
'Basic Python'
>>> soup.p
<p class="title"><b>The demo python introduces several python courses.</b></p>
>>> soup.p.string
'The demo python introduces several python courses.'
>>> type(soup.p.string)
<class 'bs4.element.NavigableString'>
>>> newsoup=BeautifulSoup("<b><!--This is a comment--></b><p>This is not a comment</p>","html.parser")
>>> newsoup.b.string
'This is a comment'
>>> newsoup.p.string
'This is not a comment'
>>> type(newsoup.p.string)
<class 'bs4.element.NavigableString'>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这个任务可以分为三个部分:使用requests和beautifulsoup4爬取虎扑体育的数据,使用openpyxl生成excel表,使用matplotlib进行数据可视化。具体步骤如下: 1. 使用requests和beautifulsoup4爬取虎扑体育的数据 首先需要确定要爬取的数据类型,比如是篮球比赛数据、足球比赛数据等等。然后找到相应的数据源,可以是虎扑体育网站上的数据、API接口等等。 以爬取NBA比赛数据为例,可以使用Python的requests发送HTTP请求,获取虎扑体育NBA比赛数据的HTML页面,再使用beautifulsoup4解析HTML页面,提取所需信息。代码示例如下: ``` import requests from bs4 import BeautifulSoup url = 'https://nba.hupu.com/schedule' response = requests.get(url) soup = BeautifulSoup(response.text, 'html.parser') # 解析数据,提取所需信息 ``` 2. 使用openpyxl生成excel表 获取到数据后,需要将数据存储到excel表中。可以使用Python的openpyxl进行excel表的生成。 代码示例如下: ``` import openpyxl # 创建工作簿和工作表 wb = openpyxl.Workbook() ws = wb.active # 写入表头 ws['A1'] = '日期' ws['B1'] = '时间' ws['C1'] = '主队' ws['D1'] = '客队' # 写入数据 for i in range(len(data)): ws.cell(row=i+2, column=1, value=data[i]['date']) ws.cell(row=i+2, column=2, value=data[i]['time']) ws.cell(row=i+2, column=3, value=data[i]['home_team']) ws.cell(row=i+2, column=4, value=data[i]['away_team']) # 保存文件 wb.save('nba_data.xlsx') ``` 3. 使用matplotlib进行数据可视化 最后可以使用Python的matplotlib进行数据可视化。可以根据需求选择不同的可视化方式,比如柱状图、折线图等等。 代码示例如下: ``` import matplotlib.pyplot as plt # 统计数据 teams = {} for i in range(len(data)): home_team = data[i]['home_team'] away_team = data[i]['away_team'] if home_team not in teams: teams[home_team] = 0 if away_team not in teams: teams[away_team] = 0 teams[home_team] += 1 teams[away_team] += 1 # 可视化数据 plt.bar(teams.keys(), teams.values()) plt.title('NBA Teams') plt.xlabel('Team') plt.ylabel('Count') plt.show() ``` 以上是一个简单的示例,实际应用中需要根据数据类型和需求进行相应的处理和可视化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值