簡體   English   中英

如何從兩個指定信息之間的字符串中獲取特定字符串

[英]How do I get a specific string from a string between two specified pieces of information

我為令人困惑的標題道歉。 我環顧四周,知道如何在兩個指定字符之間獲取字符串,但我不確定如何在短語和字符之間獲取字符串,例如src="the information i want" 在這種情況下,我希望我的起點是src=" ,端點是起點之后的第一個" 我將如何 go 在get方法中指定這些參數?

以下是我尋求幫助的 output。 我不想手動復制和粘貼第二個 URL,而是想將該字符串分配給一個變量以自動化該過程。

>>> %Run myProject.py
enter URL
https://www.instagram.com/p/CAYGHWFFp-x/
<video class="tWeCl" playsinline="" poster="https://scontent-iad3-1.cdninstagram.com/v/t51.2885-15/e35/100101005_584997515466659_2719890114744519125_n.jpg?_nc_ht=scontent-iad3-1.cdninstagram.com&_nc_cat=111&_nc_ohc=DI3B3wg_vaQAX_MvEcQ&oh=06b611ef41299d4f0278467fb1d74e94&oe=5EC66079" 
preload="none" src="https://scontent-iad3-1.cdninstagram.com/v/t50.2886-16/98205256_176119867089312_5443572653160790508_n.mp4?_nc_ht=scontent-iad3-1.cdninstagram.com&_nc_cat=100&_nc_ohc=JtZXc2HiQ9kAX_097NE&oe=5EC68ACC&oh=ac92032cb89fa1dfbcb5f2fa9016c9ba" type="video/mp4"></video>
enter the URL

太感謝了!

您可以使用Beautiful Soup來解析此內容。 然后您可以查找video元素,並讀取它們的src屬性。

from bs4 import BeautifulSoup
soup = BeautifulSoup(text, 'html.parser')
for video in soup.find_all('video'):
    print(video.get('src'))

Output

https://scontent-iad3-1.cdninstagram.com/v/t50.2886-1698205256_176119867089312_5443572653160790508_n.mp4?_nc_ht=scontent-iad3-1.cdninstagram.com&_nc_cat=100&_nc_ohc=JtZXc2HiQ9kAX_097NE&oe=5EC68ACC&oh=ac92032cb89fa1dfbcb5f2fa9016c9ba

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM