-1
如何提取<p>
段落標記中的數據和<li>
哪些屬於名爲<div>
的類?使用python beautifulsoup進行網頁爬蟲
如何提取<p>
段落標記中的數據和<li>
哪些屬於名爲<div>
的類?使用python beautifulsoup進行網頁爬蟲
使用功能find()
和find_all()
:
import requests
from bs4 import BeautifulSoup
url = '...'
r = requests.get(url)
data = r.text
soup = BeautifulSoup(data, 'html.parser')
div = soup.find('div', {'class':'class-name'})
ps = div.find_all('p')
lis = div.find_all('li')
# print the content of all <p> tags
for p in ps:
print(p.text)
# print the content of all <li> tags
for li in lis:
print(li.text)
真棒..感謝一噸:-) – pKa
交一個樣本輸入端。 –
post example html/xml –