from bs4 import BeautifulSoupurl = 'C:\\Users\\Zandrio\\Documents\\Python-Selexion\\HTML-localhost\\Selexion.html'page = open(url)soup = BeautifulSoup(page.read(), features="lxml")prettify = soup.prettifyModel = "".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。请建议任何解决方案。
1 回答
哔哔one
TA贡献1854条经验 获得超8个赞
from bs4 import BeautifulSoup
url = '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() # text of first span
print(model_number)
添加回答
举报
0/150
提交
取消