【问题标题】:how to get href from <a> when searching for text in beautiful soup and在美丽的汤中搜索文本时如何从 <a> 获取 href 和
【发布时间】:2019-07-17 18:11:01
【问题描述】:

我正在使用 selenium、BeautifulSoup 和 Selenium 来执行数据提取。

此页面已分页。我知道这个链接存在于页面的某处:

<a href="/DP/changeQueryPageAction.do?pager.offset=20">[ Next &gt; ]</a>

这个网址在页面上的随机位置,所以我需要做的是找到文本并提取href。

如何让 bs4 找到文本,并给我href?

谢谢

【问题讨论】:

    标签: python beautifulsoup


    【解决方案1】:

    要根据文本或任何其他属性查找元素,您必须使用 re 模块来获取。

    import bs4
    import re
    html_doc="""<html><a href="/DP/changeQueryPageAction.do?pager.offset=20">[ Next &gt; ]</html></a>"""
    soup = bs4.BeautifulSoup(html_doc, 'html.parser')
    Search_Text=soup.find('a' , text=re.compile("Next"))
    print(Search_Text['href'])
    

    输出:

    /DP/changeQueryPageAction.do?pager.offset=20
    

    如果它适合你,请告诉我。

    【讨论】:

      猜你喜欢
      • 2018-07-31
      • 2012-05-09
      • 2011-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-08
      相关资源
      最近更新 更多