从<a> which has a download option using python

时间:2017-07-29 02:04:07

标签: html python-2.7 web-scraping beautifulsoup

I'm trying to scrape the contents of a tag. Here is a example of the html:

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

Here is what I'm doing:

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

The print statement returns nothing. Is there something wrong with what I did?

1 个答案:

答案 0 :(得分:2)

它失败,因为while ( int n < 0 && int n > 23)内有<span>个标记,因此对象的 .string() 方法返回<a>,您可以重写您的列表使用列表理解使其工作,如:

None