I am trying to extract a link which is written like this:
<h2 class="section-heading">
<a href="http://www.nytimes.com/pages/arts/index.html">Arts »</a>
</h2>
my code is:
from bs4 import BeautifulSoup
import requests, re
def get_data():
url='http://www.nytimes.com/'
s_code=requests.get(url)
plain_text = s_code.text
soup = BeautifulSoup(plain_text)
head_links=soup.findAll('h2', {'class':'section-heading'})
for n in head_links :
a = n.find('a')
print a
print n.get['href']
#print a['href']
#print n.get('href')
#headings=n.text
#links = n.get('href')
#print headings, links
get_data()
the like "print a" simply prints out the whole <a>
line inside the <h2 class=section-heading>
i.e.
<a href="http://www.nytimes.com/pages/world/index.html">World »</a>
but when I execute "print n.get['href']", it throws me an error;
print n.get['href']
TypeError: 'instancemethod' object has no attribute '__getitem__'
Am I doing something wrong here? Please help
I couldn't find some similar case question here, my issue is a bit unique here, I am trying to extract a link that is inside a specific class names section-headings.