【发布时间】:2019-02-27 18:33:08
【问题描述】:
我正在尝试使用 python beautifulsoup 模块导出我的 LinkedIn 联系人姓名。我的代码如下:
import requests
from bs4 import BeautifulSoup
client = requests.Session()
HOMEPAGE_URL = 'https://www.linkedin.com'
LOGIN_URL = 'https://www.linkedin.com/uas/login-submit'
CONNECTIONS_URL = 'https://www.linkedin.com/mynetwork/invite-connect/connections/'
html = client.get(HOMEPAGE_URL).content
soup = BeautifulSoup(html, "html.parser")
csrf = soup.find(id="loginCsrfParam-login")['value']
login_information = {
'session_key':'username',
'session_password':'password',
'loginCsrfParam': csrf,
}
try:
client.post(LOGIN_URL, data=login_information)
print "Login Successful"
except:
print "Failed to Login"
html = client.get(CONNECTIONS_URL).content
soup = BeautifulSoup(html , "html.parser")
print soup.find_all('div', attrs={'class' : 'mn-connection-card__name'})
但问题是我总是得到一个空列表。像下面这样:
Login Successful
[]
一个html结构是这样的:
<span class="mn-connection-card__name t-16 t-black t-bold">
Sombody's name
</span>
我认为我应该改变我的 soup.x 方法。我用了 find、select、find_all 都没有成功。
谢谢
【问题讨论】:
-
你应该使用Linkedin的REST API来查询这类信息。
-
Ummmmm...当您应该尝试查找跨度(在 html 结构中)时,是否可以像您尝试查找 div(在您的代码中)一样简单?
-
@darksky 不幸的是,LinkedIn 对其 API 的权限有限,因此我需要您抓取他们的网站以收集我所需的信息。所以他们的 REST API 暂时没有用。 (我需要获取联系人的电子邮件地址、电话号码……)
-
@Mahdi 您是否按照 Jeff 的建议尝试在
soup.find_all('div', attrs={'class' : 'mn-connection-card__name'})中将'div'替换为'span'?
标签: python beautifulsoup linkedin