gpt4 book ai didi

python - 如何从 中提取链接

:BeautifulSoup

转载 作者:太空宇宙 更新时间:2023-11-04 03:18:47 24 4
gpt4 key购买 nike

我正在尝试提取这样写的链接:

<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__'

我是不是做错了什么?请帮忙

我在这里找不到类似的案例问题,我的问题在这里有点独特,我正在尝试提取特定类名部分标题内的链接。

24 4 0

Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com