python - 在某些情况下,汤很美味,但在其他情况下则不然。为什么?

标签 python json parsing beautifulsoup

我正在使用 Beautiful Soup 从 HTML 文件中解析一些 JSON。 基本上我用来从 LinkedIn 搜索结果中获取所有员工资料。 然而,由于某种原因,它不适用于拥有超过 10 名员工的公司。 这是我的代码

import requests, json
from bs4 import BeautifulSoup
s = requests.session()

def get_csrf_tokens():
    url = "https://www.linkedin.com/"
    req = s.get(url).text

    csrf_token = req.split('name="csrfToken" value=')[1].split('" id="')[0]
    login_csrf_token = req.split('name="loginCsrfParam" value="')[1].split('" id="')[0]

    return csrf_token, login_csrf_token


def login(username, password):
    url = "https://www.linkedin.com/uas/login-submit"
    csrfToken, loginCsrfParam = get_csrf_tokens()

    data = {
        'session_key': username,
        'session_password': password,
        'csrfToken': csrfToken,
        'loginCsrfParam': loginCsrfParam
    }

    req = s.post(url, data=data)
    print "success"

login(USERNAME PASSWORD)
def get_all_json(company_link):
    r=s.get(company_link)
    html= r.content
    soup=BeautifulSoup(html)
    html_file= open("html_file.html", 'w')
    html_file.write(html)
    html_file.close()
    Json_stuff=soup.find('code', id="voltron_srp_main-content")
    print Json_stuff
    return remove_tags(Json_stuff)
def remove_tags(p):
    p=str(p)
    return p[62: -10]

def list_of_employes():
    jsons=get_all_json('https://www.linkedin.com/vsearch/p?f_CC=2409087')
    print jsons
    loaded_json=json.loads(jsons.replace(r'\u002d', '-'))
    employes=loaded_json['content']['page']['voltron_unified_search_json']['search']['results']
    return employes
def get_employee_link(employes):
    profiles=[]
    for employee in employes:
        print employee['person']['link_nprofile_view_3']
        profiles.append(employee['person']['link_nprofile_view_3'])
    return profiles , len(profiles)

print get_employee_link(list_of_employes())

它不适用于现有的链接;但它适用于该公司搜索:https://www.linkedin.com/vsearch/p?f_CC=3003796

编辑: 我非常确定这是 get_all_json() 函数的错误。如果 你看一下,对于员工超过 10 人的公司,它无法正确获取 JSON。

最佳答案

这是因为结果是分页的。您需要访问 json 数据中定义的所有页面:

data['content']['page']['voltron_unified_search_json']['search']['baseData']['resultPagination']['pages']

pages 是一个列表,对于公司 2409087 来说,它是:

[{u'isCurrentPage': True, u'pageNum': 1, u'pageURL': u'http://www.linkedin.com/vsearch/p?f_CC=2409087&page_num=1'}, 
 {u'isCurrentPage': False, u'pageNum': 2, u'pageURL': u'http://www.linkedin.com/vsearch/p?f_CC=2409087&page_num=2', u'page_number_i18n': u'Page 2'}, 
 {u'isCurrentPage': False, u'pageNum': 3, u'pageURL': u'http://www.linkedin.com/vsearch/p?f_CC=2409087&page_num=3', u'page_number_i18n': u'Page 3'}]

这基本上是您需要克服并获取数据的 URL 列表。

这是您需要执行的操作(省略登录代码):

def get_results(json_code):
    return json_code['content']['page']['voltron_unified_search_json']['search']['results']

url = "https://www.linkedin.com/vsearch/p?f_CC=2409087"
soup = BeautifulSoup(s.get(url).text)

code = soup.find('code', id="voltron_srp_main-content").contents[0].replace(r'\u002d', '-')
json_code = json.loads(code)

results = get_results(json_code)

pages = json_code['content']['page']['voltron_unified_search_json']['search']['baseData']['resultPagination']['pages']
for page in pages[1:]:
    soup = BeautifulSoup(s.get(page['pageURL']).text)
    code = soup.find('code', id="voltron_srp_main-content").contents[0].replace(r'\u002d', '-')
    json_code = json.loads(code)
    results += get_results(json_code)

print len(results)

它打印 https://www.linkedin.com/vsearch/p?f_CC=240908725 - 您在浏览器中看到的确切内容。

关于python - 在某些情况下,汤很美味,但在其他情况下则不然。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23331190/

相关文章:

python - 为什么静态绑定(bind)对类和函数的作用不同?

python - 语法错误: unexpected EOF while parsing (python blockchain)

c++ - 如何发送 http 请求并检索 json 响应 C++ Boost

java - 如何在 Groovy 中将字符串作为属性传递。多级 JSON

Python OpenCV 视频格式浏览器播放

python - 有没有办法手动修改从给定数据集中学习的决策树中设置的阈值?

javascript - 如何格式化嵌套 json 数组取决于 api 请求 json 的条件

java - 字符串解析机制获取值

parsing - 在词法输入序列时指定文字的字符名称是什么?

javascript - 如何使用javascript或jquery解析这种类型的字符串?