尝试使用 BeautifulSoup 在 Python 中获取 span 的内容

from bs4 import BeautifulSoup


url = 'C:\\Users\\Zandrio\\Documents\\Python-Selexion\\HTML-localhost\\Selexion.html'


page = open(url)

soup = BeautifulSoup(page.read(), features="lxml")

prettify = soup.prettify

Model = "".join([div.text for div in soup.find_all('div', {'class' : 'title-options'})])

print(Model)

输出:


PS C:\Users\Zandrio> & C:/Users/Zandrio/AppData/Local/Programs/Python/Python38/python.exe c:/Users/Zandrio/Documents/Requests/selexion.py


                                        SQQE55Q90R


                    Merk:

                    Samsung                  Afdrukken

网页:


<div class="title-options">

<span>

 SQQE55Q90R

</span>

<span>

  Merk: Samsung                

</span>

<span class="print"> <a href="/nl/pr/samsung-qled-qe55q90r/product-sheet" class="product-file-pdf" target="_blank" title="Afdrukken">Afdrukken</a>

</span>

</div>

在这种情况下,我只想要型号,即这里的SQQE55Q90R。请建议任何解决方案。


素胚勾勒不出你
浏览 202回答 1
1回答

哔哔one

from bs4 import BeautifulSoupurl = 'C:\\Users\\Zandrio\\Documents\\Python-Selexion\\HTML-localhost\\Selexion.html'page = open(url)soup = BeautifulSoup(page.read(), features="lxml")div = soup.body.find('div', attrs={'class': 'title-options'})model_number = div.span.text.strip()&nbsp; # text of first spanprint(model_number)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python