2017-06-19 19 views
0

我想为LinkedIn查询提供最高搜索结果。使用html敏捷包从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的一些凭据 - 我如何通过这些?

  • 我试过Inspect元素来查看它的位置 - 如何遍历DOM以获得第一个结果?

回答

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 
+0

我安装HTML敏捷性包,但得到的生成错误 - JsonConvert,HttpWebRequest的,WebUtility不存在,OnPreRequest2没有重载与..PreRequestHandler相匹配 - 我应该包含哪些其他名称空间? –

+0

nvm,我不得不安装NewtonSoft.Json版本9,因为10不适用于VS2012 ..顺便说一句,我有一个简单的要求 - 从文件/表中读取html搜索变量并将输出转储到文件/表格。你有兴趣为$ 25亚马逊礼品卡编码吗? –