我將多個類值傳遞給BeautifulSoup.find_all()
。該值類似於l4 center
或l5 center
。 (即,"l4 center" | "l5 center"
)。將正則表達式傳遞給'BeautifulSoup.find_all'不起作用
soup.find_all("ul", {"class" : value)
我失敗(輸出沒有)這樣做有以下兩種解決方案:
soup.find_all("ul", {"class" : re.compile("l[4-5]\scenter")})
#OR
soup.find_all("ul", {"class" : ["l4 center", "l5 center"]})
的源代碼如下:
#!/usr/bin/env python3
from bs4 import BeautifulSoup
import bs4
import requests
import requests.exceptions
import re
### function, , .... ###
def crawler_chinese_idiom():
url = 'http://chengyu.911cha.com/zishu_8.html'
response = requests.get(url)
soup = BeautifulSoup(response.text)
#for result_set in soup.find_all("ul", class=re.compile("l[45] +center")): #l4 center or l5 center
for result_set in soup.find_all("ul", {"class", re.compile(r"l[45]\s+center")}): #nothing output
#for result_set in soup.find_all("ul", {"class" : "l4 center"}): #normal one
print(result_set)
crawler_chinese_idiom()
#[] output nothing
嘗試're.compile(r「l [45] \ s + center」)'。沒有'r',你需要使用''s'',並且'[45]'已經意味着4或者5. –
你是什麼意思*它不起作用*? – styvane
你的數據是什麼樣的? – hwnd