首页 > 解决方案 > 从 Python 中的锚标记自动获取数据

问题描述

我有一个页面,内容如下

<div class="entry">
  <p>some content></p>
   <a href="www.somelink.com">more</a>
</div>

在主网页中。我想在单击链接时提取并显示数据。我在python3中使用beautifulsoup。

标签: python-3.xbeautifulsoupanchor

解决方案


mainpage您可以使用参数显示提取的url的内容。

    code = '''<div class="entry">
              <p>some content></p>
              <a href="www.somelink.com">more</a>
              </div>'''
    soup = BeautifulSoup(code, 'html.parser')
    divtag = soup.find('div', attrs={"class": "entry"})
    a_tags = divtag.find_all('a')
    for a in a_tags:
        url = a.get('href')
        response = requests.get(url)
        mainpage = BeautifulSoup(response.text, 'html5lib')

推荐阅读