简体   繁体   中英

python, beautifulsoup) if Statement doesn't work

I want to parse an URL and text with this source:

<div class="news_list">
                <a href="/media/view.asp?idx=68230&amp;page=2&amp;kind=2">

                <img src="/media/upFiles2/2018/04/4-82(250).jpg" height="70" alt="" class="news_img">

                <span class="news_txt">영등포구, 7월까지 어린이보호구역 CCTV 환경 개선한다</span>
                </a><br>
                <a href="/media/view.asp?idx=68230&amp;page=2&amp;kind=2">영등포구가 사업비 1억5,000만여원을 투입해 오는 7월까지 어린이보호구역 내 설치된 방범용 CCTV 주변 환경을 개선한다. 구는 환경개선사업을 통해 학교폭력, 유괴 등 각종 범죄와 교통사고로부터 어린이들을 안전하게...</a> <span class="news_writer">박미영 기자 | 2018.04.07 11:38</span>
                </div>

there is no specific feature on tag 'a', so i use parent class name.
here is my code

from urllib.request import urlopen
from bs4 import BeautifulSoup

page = urlopen("http://www.boannews.com/media/t_list.asp?Page=1&kind=" )
soup = BeautifulSoup(page,"lxml")

for a in soup.find_all("a") :
    print(a.parent.get('class'))
    if a.parent.get('class') == "news_list" :
        print(a.text)
        print(a.get('href'))

when i use print(a.parent.get('class')) , i can get 'news_list'

but there is no print text or href on if Statement.

i think there seems to be no grammatical error, no mistake. i have no idea which part is wrong.

here's the result of my code

结果画面

a.parent.get('class') is returning a list (because tags can have many classes), and lists don't equal strings

Flip the if statement to check if the list contains the class

if "news_list" in a.parent.get('class', []):

As the bug in your code has been already solved here , I'd like to recommend using CSS selectors instead.

for a in soup.select('.news_list > a'):
    print(a.text)
    print(a['href'])

Notice the usage of the select method instead of find_all .

It's much cleaner than:

for a in soup.find_all('a'):
    if 'news_list' in a.parent.get('class', []):
        ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM