stripped_strings
时间: 2023-06-22 15:47:37 浏览: 45
`stripped_strings`是BeautifulSoup库中的一个方法,用于获取HTML/XML文档中所有标签内的纯文本字符串,去除了空格和换行符等非文本字符。例如:
```python
from bs4 import BeautifulSoup
html_doc = """
<html>
<body>
<p> This is a paragraph. </p>
<ul>
<li> Item 1 </li>
<li> Item 2 </li>
</ul>
</body>
</html>
"""
soup = BeautifulSoup(html_doc, 'html.parser')
for string in soup.stripped_strings:
print(repr(string))
```
输出结果为:
```
'This is a paragraph.'
'Item 1'
'Item 2'
```
相关问题
python stripped_strings
stripped_strings是BeautifulSoup库中的一个方法,用于获取HTML文本中的所有非空白字符串。它可以帮助我们快速提取出HTML中的文本内容,并自动去除空白字符。
在使用stripped_strings方法时,首先需要导入requests和BeautifulSoup库,然后通过requests库发送get请求获取网页的HTML源代码。接着使用BeautifulSoup库对HTML源代码进行解析,创建一个BeautifulSoup对象。然后通过选择器定位到目标元素,并使用stripped_strings方法来获取其中的文本内容。最后将获取到的文本内容存储到一个列表中,以便后续处理或展示。
如果需要获取HTML中的多个内容,可以使用遍历的方式,如for循环遍历soup.strings,然后使用repr(string)来打印每个非空白字符串。这样可以将去除空白字符后的文本逐个输出。
总结来说,stripped_strings方法是用于提取HTML文本中的非空白字符串的方法,可以帮助我们快速获取并处理HTML中的文本内容。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [Python爬虫之string、strings、stripped_strings、get_text和text用法区别](https://blog.csdn.net/qq_22592457/article/details/100597190)[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%"]
- *3* [Python编程-- BS4解析](https://blog.csdn.net/weixin_41905135/article/details/126220373)[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 ]
def get_page_content(detail_url): #detail_url=["https://xueshu.baidu.com/usercenter/paper/show?paperid=1b42b25b1953801074726c8b96fadd3e&site=xueshu_se"] for link in detail_url: print(link) res=requests.get(link).text soup=BeautifulSoup(res,'lxml') try: title=''.join(soup.select('.main-info > h3 > a')[0].stripped_strings) except: title="" print(title) authors=soup.select('.author_text > span > a') authors_list=[] for author in authors: authors_list.append(author.text) print(authors_list)什么意思
这段代码是一个函数,函数名为 get_page_content,函数的作用是获取传入的文章链接(detail_url)中的标题和作者列表,并将它们打印出来。具体实现过程如下:
1. 遍历传入的文章链接列表(detail_url),并打印每个链接。
2. 对于每个链接,使用 requests 库发起 GET 请求,并将返回的 HTML 文档传递给 BeautifulSoup 对象。
3. 使用 CSS 选择器获取文章标题和作者列表。如果无法找到标题,则将 title 变量设置为空字符串。
4. 将作者列表中每个作者的名称存储在 authors_list 列表中。
5. 最后,将标题和作者列表打印出来。
需要注意的是,这段代码仅仅实现了获取文章标题和作者列表的功能,如果需要获取更多的信息,需要对代码进行扩展。