[英]Is there ability to get articles from VK API?
我嘗試從 VK 組獲取文章。 但我找不到從 VK API 獲得它們的任何可能性。 也許有人面臨同樣的問題? 有沒有機會使用 get for Posts 來獲取文章? (我正在使用 vk_api python 包)
免責聲明:我基本上無法完全理解 VK API 文檔中的俄語文檔。
It seems there is no documented way to retrieve articles in VK API, though if you are already using Python and vk_api Then you can use the session instantiated in the main class. 這不會給你一篇文章,而是 HTML 本身,所以如果你需要,你必須解析它來提取文本。 這樣的東西是我在我的代碼中使用的:
import vk_api
vk_session = vk_api.VkApi(login, password)
try:
vk_session.auth(token_only=True)
except vk_api.AuthError as error_msg:
print(error_msg)
return
# Note that calls are going to be performed with the vk_session object, not the API class.
article_url = "https://vk.com/@riakatyusha-akademik-fortov-buduschee-budet-takim-kakim-my-ego-opredelim"
article_content = vk_session.http.get(article_url).text
這應該可以幫助您入門。 從這里您只需要處理 HTML 代碼。 不幸的是,在VK 方法頁面中沒有關於文章的文檔,所以我們可能無法為處理文章做太多其他事情。
下面是一些代碼,可以幫助您開始從用戶或社區頁面中提取所有文章。 這里唯一的依賴是 bs4。 我使用了 lxml 解析器,因為它是最快的,我在我的機器上安裝了它,但是如果你不想要/擁有它,你可以使用其他的,如BeautifulSoup 的文檔中所建議的那樣
這個非常簡單的方法應該可以幫助您檢索組中最近發布的 20 篇文章。 我找不到加載更多項目的方法,但看起來你需要使用 author_page.php。 不過,這看起來很困難。 可能你會在 VK_api 的音頻 class 中找到一些靈感,或者在他們的 github 中詢問。
假設您不想訪問私人組,這是代碼(我認為通過使用 VK_api 請求 session 來調用 post 和 get 方法就足以登錄 vk,但似乎您需要額外的步驟):
import requests
from bs4 import BeautifulSoup
group_url = "https://m.vk.com/@riakatyusha"
body = requests.get(group_url)
soup = BeautifulSoup(body.text, "lxml")
articles_list = soup.find_all("div", class_="author-page-article")
for article in articles_list:
# VK includes relative URLS in articles so you'd need to complete it first.
url = article.a["href"]
url = "https://m.vk.com"+url
# Optionally, we could remove the GET params you have in urls such as context&ref.
url = url.split("?")[0]
# We still might retrieve some extra info in case you'd need.
title = article.find("span", class_="author-page-article__title").text
summary = article.p.text
print(title, summary, url)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.