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

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.


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


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!!