伙计们,我不是程序员,但我正在改进,但我遇到了一些错误

时间:2019-02-28 14:01:01

标签: python-3.x

我对漂亮的汤有问题...我得到了这个错误:NoneType'对象没有属性'findAll'...当我执行下面的代码时

import requests 
from bs4 import BeautifulSoup 
def news(): 

    # the target we want to open     

    url='http://www.hindustantimes.com/top-news'



    #open with GET method 

    resp=requests.get(url) 



    #http_respone 200 means OK status 

    if resp.status_code==200: 

        print("Successfully opened the web page") 

        print("The news are as follow :-\n") 



        # we need a parser,Python built-in HTML parser is enough . 

        soup=BeautifulSoup(resp.text,'html.parser')     



        # l is the list which contains all the text i.e news  

        l=soup.find("ul",{"class":"searchNews"}) 



        #now we want to print only the text part of the anchor. 

        #find all the elements of a, i.e anchor 

        for i in l.findAll("a"): 

            print(i.text) 

    else: 

        print("Error") 


news()

0 个答案:

没有答案