python - 如何在 BeautifulSoup 中添加“href contains”条件
问题描述
我正在尝试从网页中提取链接。在做的时候,我得到了所有的链接。需要提取仅包含的页面watch?v=
import urllib.request
import urllib.parse
import urllib.error
from bs4 import BeautifulSoup
import ssl
import json
import ast
import json
import os
from urllib.request import Request, urlopen
# For ignoring SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
# Input from user
#url = input('Enter Youtube Video Url- ')
#url = 'https://www.youtube.com/watch?v=MxnkDj8PIxQ'
url = 'https://www.youtube.com/feed/trending'
# Making the website believe that you are accessing it using a mozilla browser
req = Request(url, headers={'User-Agent': 'Mozilla/5.0'})
webpage = urlopen(req).read()
# Creating a BeautifulSoup object of the html page for easy extraction of data.
soup = BeautifulSoup(webpage, 'html.parser')
html = soup.prettify('utf-8')
for a in soup.find_all('a', href=True):
print ("Found the URL:", a['href'])
我的输出
Found the URL: /watch?v=EJe3xxkzj5Y
Found the URL: /watch?v=Thf60JU8E98
Found the URL: /watch?v=Thf60JU8E98
Found the URL: /user/adityamusic
Found the URL: /channel/Muzik
我的预期输出应该只包含带有 watch?v= 的链接
Found the URL: /watch?v=EJe3xxkzj5Y
Found the URL: /watch?v=Thf60JU8E98
解决方案
您可以将正则表达式传递给href
关键字find_all
soup.find_all('a', href=re.compile('^/watch\?v=')
代码
import re
# Rest of your code ...
for a in soup.find_all('a', href=re.compile('^/watch\?v=')):
print ("Found the URL:", a['href'])
推荐阅读
- typescript - 对于具有一个或多个相同类型属性的对象,正确的 TypeScript 接口是什么?
- python - 在python中使用size()在数据框中输出为零
- python-3.x - python pyttsx3 错误 -- _pickle.UnpicklingError: invalid load key, '\x00'
- excel - CountIf 如何接受结构化参考列作为标准 VBA?
- swiftui - 如何移动列表中的房间(WWDC 2019 视频 204)?
- amazon - Spring Cloud Stream Kinesis 配置
- javascript - 如何在给定的数字范围内找到数字的所有因数
- c++ - 包含由 typedef 定义的类对象向量的模板类。如何在 main 中调用它们?
- javascript - 用户登录 HTML 时更改导航菜单
- postgresql - 如何在 Docker 容器构建期间设置 Postgres 管理员密码