【问题标题】:Scrape Twitter Embedded URL via Python通过 Python 抓取 Twitter 嵌入式 URL
【发布时间】:2016-10-25 15:38:50
【问题描述】:

我目前尝试提取嵌入 Twitter 视频中号召性用语按钮中的 URL。一个例子:

Twitter Video

在使用 Chrome Inspect 时,我可以相对轻松地发现我在寻找什么:

现在我正在尝试在 Python 中抓取该突出显示的链接。 我找不到任何从 Twitter API 获取它的方法,因此我切换到 BeautifulSoup。但是当搜索任何链接时,它不会显示给我:

In[23]: url = "https://amp.twimg.com/v/a693e53f-a6a3-4ff1-b06e-7c5402db0e06"
In[24]: resp = requests.get(url).content 
In[25]: soup = BeautifulSoup(resp, 'lxml') 
In[26]: soup.find_all('a')
Out[26]: 
[<a href="https://twitter.com/unibet" target="_blank">@unibet</a>,
<a class="download-btn" id="app-download"><img id="whiteLogo"      
src="https://amp.twimg.com/amplify-web-player/prod/styles/img/twitter_logo_white.png"/></a>]

知道我可以做些什么来提取那个嵌入的 URL 吗?非常感谢任何帮助!

【问题讨论】:

    标签: python html twitter web-scraping tweepy


    【解决方案1】:

    数据是通过 ajax 请求动态创建的,您可以使用 name="twitter:amplify:vmap" 从原始页面 meta 标记中提取 xml 的 url,然后请求 xml 格式的数据:

    ?xml version="1.0" encoding="utf-8"?>
    <vmap:VMAP xmlns:esi="http://www.edge-delivery.org/esi/1.0" xmlns:tw="http://twitter.com/schema/videoVMapV2.xsd" xmlns:vmap="http://www.iab.net/vmap-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="vast3.xsd">
    <vmap:Extensions>
    <vmap:Extension>
    <tw:amplify>
    <tw:content contentId="745543706946658305" ownerId="143820595" stitched="false">
    <tw:cta_watch_now url="https://www.unibet.co.uk/stan/campaign.do?cmpId=1042109&amp;affiliateId=52&amp;affId=5211000020&amp;adID=LINC_E2_T9&amp;unibetTarget=/luckisnocoincidence"/>
    <MediaFiles>
    <MediaFile>
                  http://amp.twimg.com/prod/multibr_v_1/video/2016/06/22/09/745543706946658305-libx264-main-2028k.mp4?5LiXscTGA2BYvqh2cKP8uTkru1N%2Fj8exRYhB9PbbFpM%3D
                </MediaFile>
    </MediaFiles>
    <tw:videoVariants>
    <tw:videoVariant content_type="application/x-mpegURL" url="https://video.twimg.com/amplify_video/745543706946658305/pl/st7wblyZRtiYtYP9.m3u8?expiration=1466688540&amp;hmac=cb919c7cbe840ad38f8892f430695245991b19022d3359a68f724754171a5874"/>
    <tw:videoVariant bit_rate="320000" content_type="video/mp4" url="https://video.twimg.com/amplify_video/745543706946658305/vid/320x180/JST5dEfLU99QyWle.mp4?expiration=1466688540&amp;hmac=0dc8d5a53cba3228ad6b01d766bf0ad0b8c8504b9cba5db93dd62e379cdad9dc"/>
    <tw:videoVariant content_type="application/dash+xml" url="https://video.twimg.com/amplify_video/745543706946658305/pl/st7wblyZRtiYtYP9.mpd?expiration=1466688540&amp;hmac=74a2b83bdc0020957b7d8603a66ae514425e25c05b546108d7667fe7345afbfb"/>
    <tw:videoVariant bit_rate="2176000" content_type="video/mp4" url="https://video.twimg.com/amplify_video/745543706946658305/vid/1280x720/U7ucLbF_u4E8CYBQ.mp4?expiration=1466688540&amp;hmac=5207d3904cb34b9fc21a584e2f47247e6e0f9a97cacb0ae5721b5f1fd9167916"/>
    <tw:videoVariant bit_rate="832000" content_type="video/mp4" url="https://video.twimg.com/amplify_video/745543706946658305/vid/640x360/Zopai0yZTfHhyq6W.mp4?expiration=1466688540&amp;hmac=fd736bdd53b487f2a881b583cd2e39610365d82970a9a0ed6c695c5eb44476b2"/>
    </tw:videoVariants>
    </tw:content>
    </tw:amplify>
    </vmap:Extension>
    </vmap:Extensions>
    <!-- We only support linear start (preroll) for now -->
    <vmap:AdBreak breakId="preroll3" breakType="linear" timeOffset="start">
    <vmap:AdSource allowMultipleAds="false" followRedirects="false" id="0">
    <vmap:VASTData>
    <VAST>
    </VAST>
    </vmap:VASTData>
    </vmap:AdSource>
    </vmap:AdBreak>
    </vmap:VMAP>
    

    所以我们只需要从中提取 url:

    from bs4 import BeautifulSoup
    import requests
    
    url = "https://amp.twimg.com/v/a693e53f-a6a3-4ff1-b06e-7c5402db0e06"
    resp = requests.get(url).content
    soup = BeautifulSoup(resp, 'lxml')
    
    xml = soup.select_one("meta[name=twitter:amplify:vmap]")["content"]
    soup2 = BeautifulSoup(requests.get(xml).content,"xml")
    
    print(soup2.find("cta_watch_now")["url"])
    

    然后给我们链接:

    https://www.unibet.co.uk/stan/campaign.do?cmpId=1042109&affiliateId=52&affId=5211000020&adID=LINC_E2_T9&unibetTarget=/luckisnocoincidence
    

    【讨论】:

    • 完美这真的很有帮助!这是动态调用嵌入式媒体内容的“标准”方式吗?它会在 Facebook 上类似地工作吗?
    • 不用担心,不幸的是,几乎每个站点都是不同的,因此您需要监控请求以查看到底发生了什么,如果您打开 chrom,chr​​ome 工具或 firebug 是抓取时必不可少的工具工具并查看网络选项卡下的 xhr 选项卡下,您可以看到获取请求。
    猜你喜欢
    • 2011-08-10
    • 2021-09-06
    • 1970-01-01
    • 2022-07-13
    • 1970-01-01
    • 2015-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多