【问题标题】:Ways to get href from instagram从 Instagram 获取 href 的方法
【发布时间】:2019-02-20 15:12:36
【问题描述】:

除了 selenium,还有什么方法可以从“https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/”获取“a href”? 在 api 的帮助下,我只能获得具有这种类型的图片的链接: https://instagram.fhel6-1.fna.fbcdn.net/vp/b6c669ed3b5be0dc9c183412d738acac/5CEC3935/t51.2885-15/e35/c119.0.842.842/s240x240/49787501_1587577534678419_6308372780046107029_n.jpg?_nc_ht=instagram.fhel6-1.fna.fbcdn.net 我不需要这个。我想得到这样的链接'https://www.instagram.com/p/BuGpLWsFioq/'。我正在尝试使用 bs4 和 'lxml' 解析器来执行此操作,但是在 html 中得到没有 'a href' 的结果。 我需要知道是否可以抓取此信息?很明显,javascript 会生成更多信息。那么,除了 selenium-webdriver 之外,这是一种抓取这些数据的方法吗?

【问题讨论】:

    标签: python api beautifulsoup instagram


    【解决方案1】:

    您要查找的所有信息都在 <script type=text/javacript>

    您可以使用以下正则表达式获取它:

    from bs4 import BeautifulSoup as soup
    import requests
    import json
    import re
    
    def _get_json_footer(html):
        s = str(html)
        r = re.compile('"entry_data":(.*?),"gatekeepers"')
        m = r.search(s)
        if m:
            result = m.group(1)
        return json.loads(result)
    
    url = 'https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/'
    page = requests.get(url)
    html = soup(page.text, 'html.parser')
    json_footer = _get_json_footer(html)
    
    tagpage = json_footer.get('TagPage')
    

    然后您可以在 tagpage 字典中导航以获取您的数据

    编辑:

    要获取帖子链接,您只需在 tagpage 字典中导航:

    from bs4 import BeautifulSoup as soup
    import requests
    import json
    import re
    
    def _get_json_footer(html):
        s = str(html)
        r = re.compile('"entry_data":(.*?),"gatekeepers"')
        m = r.search(s)
        if m:
            result = m.group(1)
        return json.loads(result)
    
    url = 'https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/'
    page = requests.get(url)
    html = soup(page.text, 'html.parser')
    json_footer = _get_json_footer(html)
    
    tagpage = json_footer.get('TagPage')
    
    links = []
    edges = tagpage[0].get('graphql',{}).get('hashtag',{}).get('edge_hashtag_to_media',{}).get('edges',[])
    for e in edges:
        links.append("https://www.instagram.com/p/"+e.get('node',{}).get('shortcode','')+'/')
    
    print(links)
    

    输出:

    ['https://www.instagram.com/p/Bsh4UcdBRvY/', 'https://www.instagram.com/p/Bq8vAMRHtGB/', 'https://www.instagram.com/p/Bn_vfeWhcYL/', 'https://www.instagram.com/p/Bm1QRb2ntWL/', 'https://www.instagram.com/p/Bj5pLHAnVuY/', 'https://www.instagram.com/p/Bfn2QWiHKK5/', 'https://www.instagram.com/p/BfC4ZnTntq0/', 'https://www.instagram.com/p/BeomaB6Hb8-/', 'https://www.instagram.com/p/vYszwjyLdB/', 'https://www.instagram.com/p/sQI6Jfpi3f/', 'https://www.instagram.com/p/sO9oXPMr6K/', 'https://www.instagram.com/p/qzvHuCHUgH/', 'https://www.instagram.com/p/WdlKcCBW3w/']

    您可以将键 edge_hashtag_to_media 更改为 edge_hashtag_to_top_posts 以获取其他值

    【讨论】:

    【解决方案2】:

    让我知道这就是你所关心的。

    from bs4 import BeautifulSoup
    import requests
    resp=requests.get("https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/")
    html = resp.content
    soup = BeautifulSoup(html,'html.parser')
    
    
    for a in soup.find_all('link',rel='alternate',href=True):
        print "Found the URL:", a['href']
    

    输出:

    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=en
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=fr
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=it
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=de
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=es
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=zh-cn
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=zh-tw
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=ja
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=ko
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=pt
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=pt-br
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=af
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=cs
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=da
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=el
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=fi
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=hr
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=hu
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=id
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=ms
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=nb
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=nl
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=pl
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=ru
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=sk
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=sv
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=th
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=tl
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=tr
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=hi
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=bn
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=gu
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=kn
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=ml
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=mr
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=pa
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=ta
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=te
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=ne
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=si
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=ur
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=vi
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=bg
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=fr-ca
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=ro
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=sr
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=uk
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=zh-hk
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=es-la
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=es-la
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=es-la
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=es-la
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=es-la
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=es-la
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=es-la
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=es-la
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=es-la
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=es-la
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=es-la
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=es-la
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=es-la
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=es-la
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=es-la
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=es-la
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=es-la
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=es-la
    Found the URL: https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/?hl=es-la
    

    【讨论】:

    猜你喜欢
    • 2021-07-14
    • 2021-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-29
    相关资源
    最近更新 更多