python is beautiful_Python 3 - 从beautifulSoup中的标签中获取文本

1586010002-jmsa.png

I am using beautifulSoup to extract data from website. Text from that website changes everytime you reload your page so basically I wish to be able to set a focus on the class name as a Static variable since the text is Dynamic.

import requests

from bs4 import BeautifulSoup

url = 'xxxxxxxxxxx'

r = requests.get(url)

soup = BeautifulSoup(r.content, 'html.parser')

class2 = soup.find_all(True, class_="template_title")

print (class2)

which prints out

4

When the page reloads, I will still have the focus on the area but I do not know how to print only the text (which in this case is : 4)

Once this is figured out, I have another question: If the class contains multiple tags, is there a way to get more static data to be sure it only prints the text I was searching for and not more? ( I have class, but could I use height="50" valign="bottom" width="535" as well?)

解决方案You can use text or string attribute of the element.

elems = soup.find_all(True, class_='template_title')

print([elem.string for elem in elems])

# prints `['4']` for the given html snippet

Specify more attributes as you want:

elems = soup.find_all(True, class_='template_title',

height='50', valign='bottom', width='535')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PythonBeautifulSoup是一个用于从HTML或XML文件数据的Python库。它提供了一种简单而灵活的方式来解析和遍历HTML或XML文档,并提所需的数据。通过BeautifulSoup,你可以使用find和find_all等方法通过查找ID获取元素的信息。可以使用get_text()方法从BeautifulSoup对象或Tag对象获取文本的结果字符串。 Beautiful Soup库的名字来自《爱丽丝梦游仙境》,它是一本著名的童话故事,而Beautiful Soup库的创造者把它比作从混乱的HTML或XML数据的过程,就像爱丽丝在梦探索仙境一样。如果想了解更多关于BeautifulSoup的相关信息,可以搜索软件开发网以前的文章或查看官方文档。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [【PythonBeautifulSoup](https://blog.csdn.net/weixin_45468845/article/details/108498707)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [PythonBeautifulSoup通过查找Id获取元素信息](https://download.csdn.net/download/weixin_38608726/14908380)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值