Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试查找href时Python BeautifulSoup错误元素不可见?_Python - Fatal编程技术网

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

尝试查找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

我正在尝试在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>

我建议通过传递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>