python - 获取字符串中另一个元素之后的元素
问题描述
我有一个来自 BeautifulSoup 分析的 html,我想提取以下内容star0
:sa2
。
>>>short_comment[1]['name']
<div class="author">
<a href="/member/?id=59465221" target="_blank">唐牛</a>
<span class="star0 sa2"></span></div>
我用正则表达式尝试了一件事: star0\s[a-zA-Z0-9]
但什么也没回来。现在我正在尝试替换<
并分隔最后一个字符串:
>>> s = s.replace('<','>')
>>> s.split('>')
['', 'div class="author"', ' ', 'a href="/member/?id=59465221" target="_blank"', '唐牛', '/a', ' ', 'span class="star0 sa2"', '', '/span', '', '/div', '']
>>> s.find("star0")
我还尝试使用 BS4 将类从与“作者”类匹配的元素中取出
headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:82.0) Gecko/20100101 Firefox/82.0'}
base_url = 'https://www.nosetime.com'
def get_perfume_as_dict(url):
print(base_url + url)
response_unicode = requests.get(base_url + url, headers=headers)
soup = BeautifulSoup(response_unicode.text, 'html.parser')
perfume = {}
perfume["short_comment"] = [
{"name": name.text,
"rating": name.span['class'][1],
"comment": comment.text} for
name,
comment in zip(
soup.find_all('div', {'class':"author"}),
soup.find_all('div', {'class':"hfshow1"}),
)
] #soup.find('li', {'id':'itemcomment'}) # soup.find_all('span ', {'class':'fav_cnt'})
但是当我启动它时它似乎陷入了一个循环:
get_perfume_as_dict("/xiangshui/350870-oulong-atelier-cologne-oolang-infini.html")
解决方案
用于BeautifulSoup
查询您的 html
前任:
from bs4 import BeautifulSoup
short_comment = """<div class="author">
<a href="/member/?id=59465221" target="_blank">唐牛</a>
<span class="star0 sa2"></span></div>"""
soup = BeautifulSoup(short_comment, "html.parser")
print(soup.find("div", {'class':'author'}).span['class'])
输出:
['star0', 'sa2']
推荐阅读
- html - 如何在网络上播放音频文件但未启用下载?
- c# - Azure .NET MVC 核心应用程序:提交表单时未重定向用户
- snowflake-cloud-data-platform - Snowflake UPDATE 表和要更新的参数化列
- webrtc - 在WebRTC中,参与者是否需要连接到同一个TURN服务器?
- swift - 如何使用 Swift Timer 调用具有返回值的函数?
- amazon-web-services - 使用 boto3 编辑现有 IAM 角色信任策略
- go - 更新 crypto-config.yaml 文件并在网络中更新
- c++ - 对话框 UI 冻结/无响应,直到所有元素都加载到我的 Qlistwidget 中。有什么办法可以解决这个问题?
- arrays - 如何使用 $1 使用 sublime 删除正则表达式中的空格?
- python - 使用两个列表创建python dict时,如果键具有多个值时的键列表,我该怎么做