如何刮取元素并存储文本,href和src,然后在我的Django模板中使用它们

时间:2016-05-05 01:02:51

标签: django beautifulsoup python-requests

我想抓取元素并存储文本,href和src,然后在我的模板中使用它们

google = requests.get(url)
bs = BeautifulSoup(google.content, 'html.parser')
d = bs.title.string

a_links = bs.findAll('a', 'entry')

links = []
for link in a_links:
    links.append((
        link.text,
        link.get('href'),
        link.get('src')
    )
)

context = {
    "links": links,
}
return render(request, 'index.html', context)

我喜欢这个

links = []
for link in a_links:
    links.append((
        link.text
    )
)

context = {
    "links": links,
}
return render(request, 'index.html', context)

我按照某人的建议将其改为此

links = [link.text for link in a_links]

context = {
    "links": links,
}
return render(request, 'index.html', context)

这不起作用

 links = [link.text,link.get('href'),link.get('src') for link in a_links]

什么是正确的语法来刮取元素,存储文本,href和src然后将它放在我的django模板中?

1 个答案:

答案 0 :(得分:1)

这取决于您希望如何存储每个三重值。例如,您可以将它们存储在python词典中:

links = [{'text': link.text,
          'href': link.get('href'),
          'src': link.get('src')
          } for link in a_links]

以后可以使用字典键访问:

for link in links:
    print link['text'], link['href'], link['src']