我在一个网站上做一个基本的星座分析程序。以下是我的代码:
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'>
:
我正在为如何使用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!
我知道评论中的答案基本上解决了你的问题,但我希望给你一些背景:
所以实际上,我通过使用
request.get
简化了您的语法,它也记录在请求文档中。并添加了.strip()
。strip
用于删除所有空白,这还包括新行、\n
和制表符\t
,它们以字符串的原始形式显示。strip()
也可以用来删除前导字符和traling字符。在还有},它们基本上分别转换为左前导空格或右尾随空格,这两个空格的作用是相同的。如果您想阅读更多的示例,可以参考here
lstrip()
和{相关问题 更多 >
编程相关推荐