的Python /硒webscraping

问题描述:

enter image description here用于data_links链路: driver.get(链接)的Python /硒webscraping

review_dict = {} 
# get the size of company 
size = driver.find_element_by_xpath('//[@id="EmpBasicInfo"]//span') 

#location = ???也需要获得这部分。

我的问题:

我想刮一个网站。我正在使用selenium/python从span中删除“501到1000员工”和“Biotech & Pharmaceuticals”,但我无法使用xpath从网站中提取文本元素。我尝试了getText,获取属性的所有内容。请帮忙!

这是每次迭代的输出:我没有得到文本值。

预先感谢您!

+1

1.你期望得到什么文字? 2.请将代码发布为文字而不是图片,它可以帮助每个想要帮助的人。 –

+0

感谢您的及时回应。我试图从范围内获得“501到1000名员工”和“生物技术与制药” –

+0

如果你知道你想要得到'尺寸'标签后面的内容,那么使用 bs4的'find()' –

看来你想要的,而不是用一些元素交互只有文字,一个解决方案是使用BeautifulSoup解析HTML的你,与selenium获得由JavaScript内置的代码,你应该先把HTML内容与html = driver.page_source ,然后你可以这样做:

html =''' 
<div id="CompanyContainer"> 
<div id="EmpBasicInfo"> 
<div class=""> 
<div class="infoEntity"></div> 
<div class="infoEntity"> 
<label>Industry</label> 
<span class="value">Woodcliff</span> 
</div> 
<div class="infoEntity"> 
<label>Size</label> 
<span class="value">501 to 1000 employees</span> 
</div> 
</div> 
</div> 
</div> 
''' # Just a sample, since I don't have the actual page to interact with. 
soup = BeautifulSoup(html, 'html.parser') 
>>> soup.find("div", {"id":"EmpBasicInfo"}).findAll("div", {"class":"infoEntity"})[2].find("span").text 
'501 to 1000 employees' 

或者,当然了,避免特定的索引和寻找<label>Size</label>,它应该是更具可读性:

>>> [a.span.text for a in soup.findAll("div", {"class":"infoEntity"}) if (a.label and a.label.text == 'Size')] 
['501 to 1000 employees'] 

使用selenium你可以做:

>>> driver.find_element_by_xpath("//*[@id='EmpBasicInfo']/div[1]/div/div[3]/span").text 
'501 to 1000 employees' 
+1

我想为整个项目使用硒而不是使用汤。该网站有一些沉重的ajax属性,我需要从该部分提取大部分信息。谢谢你的帮助! –

+0

@ Fun-zin请检查我的编辑! –

+1

非常感谢您的及时回复和耐心。我用你的硒版本,它的工作。 –