【问题标题】:Pull search result from LinkedIn using html agility pack使用 html 敏捷包从 LinkedIn 中提取搜索结果
【发布时间】:2017-11-21 04:04:27
【问题描述】:

我想拉出 LinkedIn 查询的顶部搜索结果。

在这个小提琴中:https://dotnetfiddle.net/Vtwi7g

将这个链接传递给 'html' var :
https://www.linkedin.com/search/results/index/?keywords=firstname%3Ajohn%20AND%20lastname%3Adoe%20AND%20company%3Amicrosoft%20AND%20title%3Aceo&origin=GLOBAL_SEARCH_HEADER

我想得到第一个结果: https://www.linkedin.com/in/john-doe-63803769/

  • 我猜该程序需要一些凭据才能首先登录 LinkedIn - 我如何传递这些凭据?

  • 我尝试检查元素以查看其位置 - 如何遍历 DOM 以获得第一个结果?

【问题讨论】:

    标签: c# html linkedin html-agility-pack


    【解决方案1】:

    链接搜索会更复杂。他们对未经授权的用户关闭了搜索。

    首先,您需要使用浏览器登录,然后获取会话 cookie li_at 和 _lipt。

    LinkedIn 不会将结果列表直接呈现为 html 标记。他正在将大的 json 对象渲染到 <code> 元素中,然后使用 JS 来渲染它。

    你的控制台应用应该是这样的:

    static void Main(string[] args)
    {
        var html = @"https://www.linkedin.com/search/results/index/?keywords=firstname%3Ajohn%20AND%20lastname%3Adoe%20AND%20company%3Amicrosoft%20AND%20title%3Aceo&origin=GLOBAL_SEARCH_HEADER";
    
        HtmlWeb web = new HtmlWeb();
        web.PreRequest = new HtmlWeb.PreRequestHandler(OnPreRequest2);
        var htmlDoc = web.Load(html);
    
        var codeElement = htmlDoc.DocumentNode.SelectNodes("//code[starts-with(@id,'bpr-guid')][last()]");
        var json = WebUtility.HtmlDecode(codeElement.Last().InnerText);
        var obj = JsonConvert.DeserializeObject<Rootobject>(json);
        var profiles = obj.included.Where(i => i.firstName != null);
        foreach(var profile in profiles)
        {
            Console.WriteLine("Profile Name: " + profile.firstName + ";" + profile.lastName + ";" + profile.occupation + ";https://www.linkedin.com/in/" + profile.publicIdentifier); 
        }
        Console.ReadKey();
    }
    public static bool OnPreRequest2(HttpWebRequest request)
    {
        var cookies =   "li_at={YOURCOOKIEHERE};" +
                        "_lipt={YOURCOOKIEHERE}";
        request.Headers.Add(@"cookie:" + cookies);
        return true;
    }
    
    
    public class Rootobject
    {
        public Included[] included { get; set; }
    }
    
    
    public class Included
    {
        public string firstName { get; set; }
        public string lastName { get; set; }
        public string occupation { get; set; }
        public string objectUrn { get; set; }
        public string publicIdentifier { get; set; }
    }
    

    最后会打印出来

    Profile Name: John;Doe;ceo at Microsoft;https://www.linkedin.com/in/john-doe-8102b868
    Profile Name: John;Doe;Ceo at Microsoft;https://www.linkedin.com/in/john-doe-63803769
    Profile Name: John;Doe;CEO at Microsoft;https://www.linkedin.com/in/john-doe-2151b69b
    

    【讨论】:

    • 我安装了 html 敏捷包,但出现构建错误 - JsonConvert、HttpWebRequest、WebUtility 不存在,OnPreRequest2 没有重载匹配 ..PreRequestHandler - 我应该包括哪些其他命名空间?
    • nvm,我必须安装 NewtonSoft.Json 版本 9,因为 10 不适用于 VS2012 .. 顺便说一句,我还有一个简单的要求 - 从文件/表中读取 html 搜索变量并将输出转储到文件/表中。您是否有兴趣将其编码为 25 美元的亚马逊礼品卡?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多