2017-02-16 101 views
0

我正在尝试从链接的搜索页面获取公司的行业信息。我使用Chrome的开发工具获取xpath表单,但它会返回空括号。这里似乎是什么问题?lxml找不到Chrome提供的xpath?

from lxml import html 
import requests 

page = requests.get('https://www.linkedin.com/search/results/companies/?keywords=cisco.com') 
tree = html.fromstring(page.content) 

industry = tree.xpath('//*[@id="ember3734"]/div/div[1]/p[1]') 

print(industry) 

回答

1

我用selenium和phantomjs制作了脚本,因为网站使用了很多javascript。

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 

import lxml.html 
import re 
from selenium import webdriver 
from time import sleep 
from selenium.webdriver import DesiredCapabilities 
from pprint import pprint 

desired_capabilities = DesiredCapabilities.PHANTOMJS.copy() 
desired_capabilities['phantomjs.page.customHeaders.User-Agent'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) ' \ 
                    'AppleWebKit/537.36 (KHTML, like Gecko) ' \ 
                    'Chrome/39.0.2171.95 Safari/537.36' 
driver = webdriver.PhantomJS(desired_capabilities=desired_capabilities) 

username = '[email protected]' 
password = 'password' 


# driver = webdriver.PhantomJS() 
driver.set_window_size(1120, 550) 
driver.get("https://www.linkedin.com") 

driver.find_element_by_id('login-password').send_keys(password) 
driver.find_element_by_id('login-email').send_keys(username) 
driver.find_element_by_id("login-submit").click() 
driver.get("https://www.linkedin.com/search/results/companies/?keywords=cisco.com") 
sleep(3) 
html = driver.page_source 
root = lxml.html.fromstring(html) 

reg = re.compile('ember-view\">\s+<h3\s+class=\"search\-result__title\s+Sans\-17px\-black\-85\%\-semibold-dense\">(.*?)<\/h3>') 
names = reg.findall(html) 

pprint(names) 

driver.quit() 

enter image description here

+0

非常感谢!我可以使用任何驱动程序还是必须使用PhantomJS? – opamp

+0

我认为Phantomjs更好,但是是个人意见。你将会得到与Firefox或谷歌浏览器相同的结果。但我认为phantomjs更轻。 – wu4m4n

+0

@opamp您可以使用此https://gist.github.com/Wu4m4n/597367d32e443b9fe120f47d78d56bce安装phantomjs – wu4m4n

0

我觉得这个页面是由JavaScript生成的。由于请求在不执行JavaScript的情况下下载页面,因此只会获得主页面/模板,而不是您期望的数据。

尝试在Chrome浏览器中查看源页面进行确认。

+0

谢谢你的评论。当我在我的Mac上时,我会尝试一下。那么在这种情况下,我将如何处理我需要的数据呢? – opamp