How to use CSS selectors to retrieve specific links lying in some class using BeautifulSoup?

后端 未结 3 1207
既然无缘
既然无缘 2020-12-13 14:23

I am new to Python and I am learning it for scraping purposes I am using BeautifulSoup to collect links (i.e href of \'a\' tag). I am trying to collect the links under the \

相关标签:
3条回答
  • 2020-12-13 14:32
    soup.select('div')
    # All elements named <div>
    
    soup.select('#author')
    # The element with an id attribute of author
    
    soup.select('.notice')
    # All elements that use a CSS class attribute named notice
    
    soup.select('div span')
    # All elements named <span> that are within an element named <div>
    
    soup.select('div > span')
    # All elements named <span> that are directly within an element named <div>,
    # with no other element in between
    
    soup.select('input[name]')
    # All elements named <input> that have a name attribute with any value
    
    soup.select('input[type="button"]')
    # All elements named <input> that have an attribute named type with value button
    

    You may also be interested by this book.

    0 讨论(0)
  • 2020-12-13 14:51

    The page is not the most friendly in the use of classes and markup, but even so your CSS selector is too specific to be useful here.

    If you want Upcoming Events, you want just the first <div class="events-horizontal">, then just grab the <div class="title"><a href="..."></div> tags, so the links on titles:

    upcoming_events_div = soup.select_one('div#events-horizontal')
    for link in upcoming_events_div.select('div.title a[href]'):
        print link['href']
    

    Note that you should not use r.text; use r.content and leave decoding to Unicode to BeautifulSoup. See Encoding issue of a character in utf-8

    0 讨论(0)
  • 2020-12-13 14:53
    import bs4 , requests
    
    res = requests.get("http://allevents.in/lahore/")
    soup = bs4.BeautifulSoup(res.text)
    for link in soup.select('a[property="schema:url"]'):
        print link.get('href')
    

    This code will work fine!!

    0 讨论(0)
提交回复
热议问题