尝试查找href时Python BeautifulSoup错误元素不可见?
我正在尝试在href中查找包含“.ics”的URL。前几天我测试了这段代码,它工作得很好,但现在当我尝试搜索“链接中的链接”时,“打印链接”的结果是:`尝试查找href时Python BeautifulSoup错误元素不可见?,python,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
<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>
我建议通过传递css
href
选择器和正则表达式模式来简化搜索:
<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>
links = soup.find_all('a', {'href' : re.compile('.*\.ics') })
输出:
<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>
[<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>]
[,,
]
您现在不必跳转来验证锚定标记。我建议通过传递css
href
选择器和正则表达式模式来简化搜索:
<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>
links = soup.find_all('a', {'href' : re.compile('.*\.ics') })
输出:
<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>
[<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>]
[,,
]
您现在不必跳转来验证锚定标记。为什么在函数外部有一个
return
?好吧,在显示的代码中,没有函数,除此之外,为什么在for
循环中调用break
?此中断
将阻止循环多次运行,从而打印更多链接。确定。运行完全相同的脚本,但删除返回值
,会向我输出您想要的内容,即url中带有“.ics”的url。可能不会,它会打印url:https://registrar.fas.harvard.edu/calendar/upcoming/all/export.ics
对我来说,但是coldspeed的答案非常合适!=)@ViníciusAguiar谢谢你的评论,我感谢随时有第二双眼睛!为什么在函数外有一个return
?此中断
将阻止循环多次运行,从而打印更多链接。确定。运行完全相同的脚本,但删除返回值
,会向我输出您想要的内容,即url中带有“.ics”的url。可能不会,它会打印url:https://registrar.fas.harvard.edu/calendar/upcoming/all/export.ics
对我来说,但是coldspeed的答案非常合适!=)@ViníciusAguiar谢谢你的评论,我感谢随时有第二双眼睛@COLDSPEED真棒,非常感谢!只要时间允许,我会接受的to@MQ1217很乐意帮忙。:)@COLDSPEED真棒,非常感谢!只要时间允许,我会接受的to@MQ1217很乐意帮忙。:)
<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>