Question to reduce links through vk.cc, I suspect that I missed something, in response I get: '3975786006921<!>profile.css,page.css,profile.js,page.js<!>0<!>6762<!>8<!>Невозможно сократить эту ссылку.<!><!>57500570' To any address. The API does not seem to be; I didn’t have an extension for Chrome, I don’t know if it works now, the code is below:

 import requests import re def get_form_action(html): form_action = re.findall(r'<form(?= ).* action="(.+)"', html) if form_action: return form_action[0] auth_session = requests.Session() def authorization(phone, password): url = 'https://m.vk.com' response = auth_session.get(url) login_form_action = get_form_action(response.text) if not login_form_action: raise Exception('VK changed login flow') login_form_data = { 'email': phone, 'pass': password } response = auth_session.post(login_form_action, login_form_data) def get_short(link): url = 'https://vk.com/cc?act=shorten&al=1&link='+link headers = {"Content-Type": "application/x-www-form-urlencoded", "X-Requested-With": "XMLHttpRequest"} response = auth_session.post(url=url, headers=headers) return response.text 

Who can did server implementation of reduction of links through VK? I would be grateful for the help!

  • You send the parameters in the link, but in the body of the POST request. And forget to screen link too - andreymal
  • one
    url = ' vk.com/cc ' data = {'act': 'shorten', 'al': 1, 'link': 'test.com'} headers = {"Content-Type": "application / x- www-form-urlencoded "," X-Requested-With ":" XMLHttpRequest "} response = auth_session.post (url, data, headers = headers) - Igor Lavrynenko
  • one
    Changed, but still get the message. Could not shorten this link, could you give me an example of how to fix my code? - Igor Lavrynenko

1 answer 1

I added an example of how to log in and use vk to get a short link:

 def get_short_link_from_vk(login: str, password: str, link: str) -> str: """ Функция для получения короткой ссылки используя сервис vk. """ def get_form_action(html: str) -> str: """ Функция вернет ссылку для запроса авторизации """ import re form_action = re.findall(r'<form(?= ).* action="(.+)"', html) if form_action: return form_action[0] import requests session = requests.Session() # Без авторизации не получится воспользоваться страницей укорачивания ссылок url = 'https://m.vk.com' rs = session.get(url) print(rs) login_form_action = get_form_action(rs.text) if not login_form_action: raise Exception('VK changed login flow') login_form_data = { 'email': login, 'pass': password, } rs = session.post(login_form_action, login_form_data) print(rs) # Страница нужна чтобы получить hash для запроса rs = session.get('https://vk.com/cc') import re match = re.search(r"Shortener\.submitLink\('(.+)'\)", rs.text) if match is None: raise Exception('Не удалось получить hash для Shortener') shortener_hash = match.group(1) # Данные для POST запроса для получения короткой ссылки data = { 'act': 'shorten', 'link': link, 'al': '1', 'hash': shortener_hash, } headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0', 'X-Requested-With': 'XMLHttpRequest', 'Content-Type': 'application/x-www-form-urlencoded', } rs = session.post('https://vk.com/cc', headers=headers, data=data) print(rs) from bs4 import BeautifulSoup root = BeautifulSoup(rs.content, 'lxml') a_short_link = root.select_one('.shortened_link.shorten_list_header > a[href]') return a_short_link['href'] 

Using:

 LOGIN = '<LOGIN>' PASSWORD = '<PASSWORD>' link = 'https://ru.stackoverflow.com/questions/648230' short_link = get_short_link_from_vk(LOGIN, PASSWORD, link) print(short_link) link = 'https://git-scm.com/book/ru/v1/%D0%9E%D1%81%D0%BD%D0%BE%D0%B2%D1%8B-Git-%D0%9F%D1%80%D0%BE%D1%81%D0%BC%D0%BE%D1%82%D1%80-%D0%B8%D1%81%D1%82%D0%BE%D1%80%D0%B8%D0%B8-%D0%BA%D0%BE%D0%BC%D0%BC%D0%B8%D1%82%D0%BE%D0%B2' short_link = get_short_link_from_vk(LOGIN, PASSWORD, link) print(short_link) 

Console:

 <Response [200]> <Response [200]> <Response [200]> https://vk.cc/6sYwPq <Response [200]> <Response [200]> <Response [200]> https://vk.cc/5AJUvX 

Used third-party modules:

 pip install requests pip install bs4 
  • one
    Many thanks for the help, missed the request hash, everything works! - Igor Lavrynenko
  • one
    @IgorSergeevich but I didn’t understand at first what was the matter, then I noticed the hash and tried to link the link (md5, sha1), but it didn’t closely match, then I searched on the page itself and found it, and realized that it comes from vk itself and it needs pull out for the query :) - gil9red