<分区> 分区>
我正在尝试提取这样写的链接:
<h2 class="section-heading">
<a href="http://www.nytimes.com/pages/arts/index.html">Arts »</a>
</h2>
我的代码是:
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()
类似“print a”的简单打印出整个<a> <h2 class=section-heading> 里面的线即
<a href="http://www.nytimes.com/pages/world/index.html">World »</a>
但是当我执行“print n.get['href']”时,它会抛出一个错误;
print n.get['href']
TypeError: 'instancemethod' object has no attribute '__getitem__'
我是不是做错了什么?请帮忙
我在这里找不到类似的案例问题,我的问题在这里有点独特,我正在尝试提取特定类名部分标题内的链接。
