如何使用漂亮的汤提取 HTML 中特定键的值

我想将 videoId 旁边的值提取到一个变量中。


var mgPerformanceTimingSettings = {

    pageType : '...',

    cdn : '...',

    videoId : '1111111111',  //Video ID

    playerVersion : '2.1.0' // Current version

};

我的代码目前如下:


import requests

from bs4 import BeautifulSoup


url = 'url destination link'


response = requests.get(url)


soup = BeautifulSoup(response.content, "html.parser")

name_box = soup.find()

我希望我的 name_box 返回 videoId '1111111111'


有没有办法更改我的编码以包含我写到函数中的所有内容而不是程序步骤来返回我想要的输出?


心有法竹
浏览 154回答 1
1回答

慕村225694

它可以使用正则表达式而不是 BeautifulSoup 来完成import re....    videoId = re.search("videoId\s+:\s+'([^']+)", response.text).group(1)print(videoId)# 1111111111
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python