首页 > 解决方案 > 抓取Linkedin搜索结果

问题描述

使用 Selenium,我想对linkedin 执行搜索并从结果中获取帖子的链接。

但是我一直在寻找帖子链接的xpath。我在 DOM 中找不到任何代表帖子网址的链接。

我所知道的就是这个://ul[contains(@class, 'search-result')] /li它代表了结果中的帖子。

搜索结果示例:https ://www.linkedin.com/search/results/content/?keywords=stackoverflow&origin=GLOBAL_SEARCH_HEADER

你能帮我找出帖子链接的xpath吗?或者获取帖子链接的方法?

PS:我也对其他方式持开放态度,例如 HTTP 请求。不仅仅是硒。

标签: seleniumxpath

解决方案


LinkedIn 具有内部 API (Voyager),因此您可以使用它来获取结果并解析它们。响应是json,但是很复杂。

在这里,您可以如何向 Voyager API 请求搜索结果(原型):

from urllib.parse import quote
import requests

count = 10
query = 'coca cola'  #  TODO: Query to search
li_at = '{li_at from account cookie}' # TODO: Add cookie
cursor = 0  # Starts with 0, next can be acquired from responses


# Get results
search_query = '+'.join(quote(word) for word in query.split())  # Encoding search_query

url = 'https://www.linkedin.com/voyager/api/search/blended?count={}&filters=List(sortBy-%3Edate_posted,resultType-%3ECONTENT)&keywords={}&origin=SORT_RESULTS&q=all&queryContext=List()&start={}'.format(
    count, search_query, cursor)

headers = {
    "user-agent": "Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36",  # TODO
    "csrf-token": "ajax: 0",
    "x-restli-protocol-version": "2.0.0",
}
cookies = {'liap': 'true',
           'JSESSIONID': 'ajax: 0',
           'li_at': li_at
          }

r = requests.get(url, cookies=cookies, headers=headers, timeout=15)
response = r.json()

# Parse links
found_links = list()
for post in response['elements'][1]['extendedElements']:  # Here we got all posts with a lot of data
    action_types = post['update']['value']['com.linkedin.voyager.feed.render.UpdateV2']['updateMetadata']['actions']  # List of possible actions with post (for web-client)
    for action in action_types:  # We need to find RESHARE action - it contains full link to post
        if "SHARE_VIA" in action['actionType']:
            found_links.append(action['url'])
            
# Results
print(found_links)

您需要更正您帐户中的 User_Agent(或使用我的)和 'li_at' cookie。

结果将按日期排序。


推荐阅读