html - 如何使用 Beautifulsoup4 跳过使用相同元素的抓取
问题描述
我想从网页上抓取视频,但在该页面中有两个iframe标签。一个用于显示 Facebook 页面,另一个用于嵌入视频。我只想从中获取视频网址。但是当我尝试抓取时,我得到了所有 iframe ..
像这样:
url_videos = requests.get(link_to_video)
video_link = BeautifulSoup(url_videos.text, 'html.parser')
video_on_iframe = video_link.find('iframe')
print(video_on_iframe)
当我尝试运行上面的代码时,我得到了这个结果:
<iframe allow="encrypted-media" allowtransparency="true" frameborder="0" height="80" scrolling="no" src="https://www.facebook.com/plugins/page.php?href=https%3A%2F%2Fwww.facebook.com%2FAnimeindoFans%2F&tabs&width=280&height=180&small_header=true&adapt_container_width=true&hide_cover=true&show_facepile=false&appId=123434497681677" style="border:none;overflow:hidden" width="280"></iframe>
<iframe allow="encrypted-media" allowtransparency="true" frameborder="0" height="80" scrolling="no" src="https://www.facebook.com/plugins/page.php?href=https%3A%2F%2Fwww.facebook.com%2FAnimeindoFans%2F&tabs&width=280&height=180&small_header=true&adapt_container_width=true&hide_cover=true&show_facepile=false&appId=123434497681677" style="border:none;overflow:hidden" width="280"></iframe>
<iframe allow="encrypted-media" allowtransparency="true" frameborder="0" height="80" scrolling="no" src="https://www.facebook.com/plugins/page.php?href=https%3A%2F%2Fwww.facebook.com%2FAnimeindoFans%2F&tabs&width=280&height=180&small_header=true&adapt_container_width=true&hide_cover=true&show_facepile=false&appId=123434497681677" style="border:none;overflow:hidden" width="280"></iframe>
<iframe frameborder="0" height="380" scrolling="no" src="http://www.mp4upload.com/embed-q7xxgge1yu1c.html" type="text/html" width="640">
</iframe>
<iframe allow="encrypted-media" allowtransparency="true" frameborder="0" height="80" scrolling="no" src="https://www.facebook.com/plugins/page.php?href=https%3A%2F%2Fwww.facebook.com%2FAnimeindoFans%2F&tabs&width=280&height=180&small_header=true&adapt_container_width=true&hide_cover=true&show_facepile=false&appId=123434497681677" style="border:none;overflow:hidden" width="280"></iframe>
<iframe allow="encrypted-media" allowtransparency="true" frameborder="0" height="80" scrolling="no" src="https://www.facebook.com/plugins/page.php?href=https%3A%2F%2Fwww.facebook.com%2FAnimeindoFans%2F&tabs&width=280&height=180&small_header=true&adapt_container_width=true&hide_cover=true&show_facepile=false&appId=123434497681677" style="border:none;overflow:hidden" width="280"></iframe>
我不需要那个 Facebook iframe ,我只需要来自其他iframe的视频 URL和属性height="380"
和width="280"
当我尝试在find()方法中指定更多详细信息时,如下所示:
video_on_iframe = video_link.find('iframe', width=640, height=380)
我懂了:
None
None
None
<iframe frameborder="0" height="380" scrolling="no" src="http://www.mp4upload.com/embed-q7xxgge1yu1c.html" type="text/html" width="640">
</iframe>
None
None
一个iframe元素,其他没有。
所以..我的问题是如何找到所有iframe', width=640, height=380
价值并跳过None
其他价值..?
解决方案
video_on_frame = video_link.find_all('iframe', height = '380')## This means I wanna scrape iframe who has height value 380 . You can also use widht. link_array = [] for link in video_on_frame: ## Your html has 1 iframe in video_on_frame format. get_iframe_url = link['src'] ## find iframe's src try: link_array.append(get_iframe_url) ## add src into a array except: link_array.append('Error')
print(link_array) 将显示您的网址您想要什么
推荐阅读
- ruby-on-rails - ActiveAdmin 过滤器:一个 has_many:通过属性
- python - 右键单击上下文菜单时访问错误的目录
- asp.net - 我无法让 Linq Order By 处理子列表
- node.js - Keycloak node.js 适配器不会在注销时使 connect.sid 会话 cookie 无效
- javascript - 在使用 React 钩子验证新状态时修改上游状态
- ruby-on-rails - 尝试使用 Ruby on Rails 检测 mime 类型
- python - 获取对象Django的关系模型名称
- r - 使用了连续 X 变量但仍然出现错误:StatBin 需要连续 x 变量
- vmware-clarity - VMWare Clarity - 主分支 - `npm run angular:dev:start` 失败并出现错误:找不到任务:“'core:build:ts”
- oauth-2.0 - 我应该在我的应用程序中使用哪种 OAuth 流程来集成 Google Drive?