我正在使用 bs4 來獲取 web 的 html 標簽:
html = BeautifulSoup(requests.get(temp_cat_link).text, 'html.parser')
items =html.findAll('h4',{'class':'item-title font-weight-normal '})# this tag have a tag name contain white space at the end
但是當我檢查它時實際上并沒有得到所有標簽,因為有些標簽名稱最后沒有空格。它只是回傳item-title font-weight-normal 標簽。所以我把我的代碼改成這樣:
html = BeautifulSoup(requests.get(temp_cat_link).text, 'html.parser')
items =html.findAll('h4',{'class':'item-title font-weight-normal'})# this tag name doesn't contain white space at the end
但它只獲取所有標簽item-title font-weight-normal。這里的問題是我如何才能在 html 標簽中實際獲得名稱的相同字串部分的所有標簽
item-title font-weight-normal并且
item-title font-weight-normal
只有一行html.findAll
uj5u.com熱心網友回復:
您可以使用正則運算式來匹配帶有或不帶有尾隨空格的字串:
import re
from bs4 import BeautifulSoup
html = BeautifulSoup(requests.get(temp_cat_link).text, 'html.parser')
items = html.findAll('h4',{'class':re.compile(r'item-title font-weight-normal\s*')})
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/361669.html
上一篇:無法抓取發現銀行頁面
