【问题标题】:Extracting href from <a> which has a download option using python [duplicate]使用python从具有下载选项的<a>中提取href [重复]
【发布时间】:2018-01-05 04:12:37
【问题描述】:

我正在尝试抓取标签的内容。这是一个html的例子:

<p><a href="https://requiredlink.com" download>Download<span class="caret">

这是我正在做的事情:
r = requests.get("https://abc.efg.questions").content
    html_obj = html.fromstring(r)   
    soup = BeautifulSoup(r)
    for a in soup.find_all("a", text=re.compile("Download")):
        print a['href']

打印语句不返回任何内容。我的做法有问题吗?

【问题讨论】:

    标签: html python-2.7 web-scraping beautifulsoup


    【解决方案1】:

    它失败是因为&lt;a&gt;里面有一个&lt;span&gt;标签,因此对象的.string()方法返回None,你可以用列表解析重写你的列表以使其工作,例如:

    >>> for a in [s for s in soup.find_all("a") if s.text == "Download"]:
            print(a['href'])    
    
    https://requiredlink.com
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-18
      • 2019-04-09
      • 1970-01-01
      • 2020-12-20
      • 1970-01-01
      • 2019-10-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多