我有以下
li class = 'EntityList-item EntityList-item--Regular EntityList-item--n1 bp-radix__faux-anchor'
我正在嘗試匯出所有 li 類,但 n1 部分正在更改為 n2,n3 ... n100 嘗試這樣做:
url = 'https://www.xxxx.com' # just a random website
result = requests.get(url).text
doc = BeautifulSoup(result, 'html.parser')
doc
x = 1
for add in doc:
add.find('li', class_ = f'EntityList-item EntityList-item--Regular EntityList-item--n{x} bp-radix__faux-anchor')
x = 1
print(add)
但我收到錯誤訊息:TypeError: find() 沒有關鍵字引數
關于如何遍歷上述類以匯出所有元素的任何建議,直到 x 達到 100。
uj5u.com熱心網友回復:
你呼叫的是內置函式 find 而不是湯。你需要做:
soup.find('li', class_ = f'EntityList-item EntityList-item--Regular EntityList-item--n{x} bp-radix__faux-anchor')
要找到它們,請嘗試在沒有回圈的情況下使用 findAll:
soup.findAll('li', class_ = f'EntityList-item EntityList-item--Regular EntityList-item--n{x} bp-radix__faux-anchor')
我發現了你的問題:
url = 'https://www.xxxx.com' # just a random website
result = requests.get(url).text
doc = BeautifulSoup(result, 'html.parser')
doc
for i in range(101):
print(soup.find('li', class_ = f'EntityList-item EntityList-item--Regular EntityList-item--n{i} bp-radix__faux-anchor'))
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/415145.html
標籤:
