logo
Loading...

find_all()和find()和for的配合 - Cupoy

您好在D12範例中soup.find(class_="part_list_2").find_all(...

pycrawler,pycrawler-d12

find_all()和find()和for的配合

2019/12/14 下午 02:19
Python網路爬蟲討論版
葉碩涵
觀看數:9
回答數:1
收藏數:0
pycrawler
pycrawler-d12

您好

在D12範例中

soup.find(class_="part_list_2").find_all('h3')

這樣是有內容的


但我嘗試寫成

p=soup.find('div', class_='part_list_2')
tag
="<class 'bs4.element.Tag'>"
for t in p:
   
if(str(type(t)) == tag):
       allh3
=t.find_all('h3')
       
print(allh3)
       alla
=t.find_all('a')
       
print(alla)

alla是有內容的但allh3並沒有內容

不知道為什麼用了for迴圈的寫法是錯的?

程式碼網址↓

https://github.com/yehbi/1st-PyCrawlerMarathon/blob/master/Day012_Sample.ipynb

回答列表

  • 2019/12/15 下午 06:03
    張維元 (WeiYuan)
    贊同數:1
    不贊同數:0
    留言數:1

    嗨,你的 t 本身就是 h3 ,find 是去找 t 底下的物件。