美体脱衣白

2024-09-28 20:46:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我在一个网站上做一个基本的星座分析程序。以下是我的代码:

import requests
from bs4 import BeautifulSoup as bs

url = "https://www.astrospeak.com/horoscope/capricorn"

response = requests.request("GET", url)

soup = bs(response.text, 'html.parser')

locater = soup.select("#sunsignPredictionDiv > div.fullDIV > div.lineHght18 > div")

quote = locater[0].previousSibling

这给我留下了以下<class 'bs4.element.NavigableString'>

^{pr2}$

我正在为如何使用bs4上的beauthulsoupstripped_strings生成器而苦苦挣扎。元素。NavigableString. 最后我只想得到一个字符串You are working towards yet another dream and as you pursue this vision there's no doubt in your mind that it will come to fruition. It's written in the stars!


Tags: inimportdivurlbs网站responseas
1条回答
网友
1楼 · 发布于 2024-09-28 20:46:29

我知道评论中的答案基本上解决了你的问题,但我希望给你一些背景:

import requests
from bs4 import BeautifulSoup as bs

url = "https://www.astrospeak.com/horoscope/capricorn"
response = requests.get(url)
soup = bs(response.text, 'html.parser')
locater = soup.select("#sunsignPredictionDiv > div.fullDIV > div.lineHght18 > div")

quote = locater[0].previousSibling.strip()

所以实际上,我通过使用request.get简化了您的语法,它也记录在请求文档中。并添加了.strip()strip用于删除所有空白,这还包括新行、\n和制表符\t,它们以字符串的原始形式显示。strip()也可以用来删除前导字符和traling字符。在

还有lstrip()和{},它们基本上分别转换为左前导空格或右尾随空格,这两个空格的作用是相同的。如果您想阅读更多的示例,可以参考here

相关问题 更多 >