我一段时间以来一直在尝试抓取 LinkedIn 的工作机会部分,但无济于事。顺便说一句,我知道该网站有自己的 API,但我想用 Beautiful Soup 来做,因为我不久前学过,它是为了练习的目的。
这是我的代码:
import requests
from bs4 import BeautifulSoup
client = requests.Session()
HOMEPAGE_URL = 'https://www.linkedin.com'
LOGIN_URL = 'https://www.linkedin.com/login/en'
URL = 'https://www.linkedin.com/jobs/search/?geoId=101174742&keywords=data%20analyst&location=Canada'
html = client.get(HOMEPAGE_URL).content
soup = BeautifulSoup(html, "html.parser")
login_information = {
'session_key':'<username>',
'session_password':'<password>',
'loginCsrfParam': '<csrftoken>',
}
try:
p = client.post(LOGIN_URL, data=login_information)
print ("Login Successful")
except:
print ("Failed to Login")
到这里为止一切都很好。我得到“登录成功”,但当我询问“状态代码”时,我得到 403:
p.status_code
Output: 403
当然我无法抓取任何信息。我怎样才能以正确的方式做到这一点?
LinkedIn 上的网页抓取非常困难,因此需要一位 LinkedIn 数据抓取 的专业人士,他可以精确地完成所有网页抓取,而不会违反 LinkedIn 合规性。
您实际上不必重新发明轮子。有一个名为“惊喜,惊喜”linkedin-api 的模块,用于通过所谓的
Voyager
服务访问各种 LinkedIn 数据(包括职位)。
使用示例:
from linkedin_api import Linkedin
# Authenticate using any Linkedin account credentials
api = Linkedin('[email protected]', '*******')
# GET a profile
profile = api.get_profile('billy-g')
# GET a profiles contact info
contact_info = api.get_profile_contact_info('billy-g')
# GET 1st degree connections of a given profile
connections = api.get_profile_connections('1234asc12304')
我分享这个是因为你可能很难用旧的
BeautifulSoup
和 requests
来抓取 LinkedIn。另外,请注意,请勿使用您的个人帐户在 LinkedIn 上进行任何抓取活动。