Способы получить HREF от Instagram - PullRequest
0 голосов
/ 20 февраля 2019

Есть ли способы получить 'href' от 'https://www.instagram.com/explore/tags/SOMEHASHTAGHERE/', кроме селена?С помощью 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 ', чтобы сделать это, но получить результат без NO' a href 'в html.Мне нужно узнать, возможно ли очистить эту информацию или нет?Понятно, что javascript генерирует больше информации.Таким образом, таким образом это способ очистить эти данные, кроме селен-webdriver?

Ответы [ 2 ]

0 голосов
/ 20 февраля 2019

Дайте мне знать, что вы ищете.

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
0 голосов
/ 20 февраля 2019

вся информация, которую вы ищете, находится внутри <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 dictчтобы получить ваши данные

РЕДАКТИРОВАТЬ:

Чтобы получить ссылку на сообщения, вы можете просто перейти внутри tagpage dict:

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, чтобы получить другие значения

...