Python BeautifulSoup错误元素在尝试查找href时不可见?

2024-09-29 23:33:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试在href中查找包含“.ics”的URL。前几天我测试了这个代码,它运行得很好,但是现在当我尝试搜索“链接中的链接”时,“打印链接”的结果是:``

<a class="element-invisible element-focusable" href="#main-content" 
tabindex="1">Skip to main content</a>
<a class="element-invisible element-focusable" href="#main-content">Skip to 
main content</a>

因此,如果链接。获取('href')'代码永远不会满足,并且不会返回URL。是什么导致了这种情况,是否有其他方法返回包含“.ics”的URL?你知道吗

page = requests.get('https://registrar.fas.harvard.edu/calendar').content
soup = bs4.BeautifulSoup(page, 'lxml')

links = soup.find_all('a')
#print links    
for link in links:
    print link    

    if link.get('href') != None and '.ics' in link.get('href'):
        endout = link.get('href')

        if endout[:6] == 'webcal':
            endout ='https' + endout[6:]
        print
        print 'URL: ' + endout
        print
        return endout
    break

Tags: 代码urlget链接mainlinkelementlinks
1条回答
网友
1楼 · 发布于 2024-09-29 23:33:04

我建议通过传递csshref选择器和regex模式来简化搜索:

links = soup.find_all('a', {'href' : re.compile('.*\.ics') })

输出:

[<a class="subscribe" href="https://registrar.fas.harvard.edu/calendar/upcoming/all/export.ics">subscribe</a>,
 <a class="ical" href="https://registrar.fas.harvard.edu/calendar/upcoming/all/export.ics">iCal</a>]

你现在就不需要跳转来验证你的锚定标签了。你知道吗

相关问题 更多 >

    热门问题