如何从selenium的网站上获取单个公司名称

时间:2015-09-27 15:10:03

标签: python excel selenium

我是硒与蟒蛇的菜鸟。我试图从这个链接中获取一些数据:http://www.dcciinfo.com/dirinfo/companies/all/10888?start=30,我想获取的数据是公司名称并放入excel,同时复制链接并将其粘贴到同一个excel列中。接下来它应该进入下一个提到的公司。有人可以帮帮我,给我一些如何从一家公司搬到另一家公司的提示。我无法使用id,xpath识别。同样必须在下一页重复,并为所有65页完成。

我刚刚在selenium中写了基本代码:

browser = webdriver.Firefox() # Get local session of firefox
browser.get("http://www.dcciinfo.com/dirinfo/companies/all/10888?start=30") # Load page
x=browser.find_element_by_xpath("//*[@id='content']/form/div[1]/div[4]/div[1]")

2 个答案:

答案 0 :(得分:0)

使用.find_elements_by_xpath这将返回元素列表。然后你可以循环它。也可以使用此//*[@id="content"]/form/div/div/div[1]/a代替当前的xpath

答案 1 :(得分:0)

我更喜欢使用XP选择器的CSS选择器。这应该引导你朝着正确的方向前进。

browser.get("http://www.dcciinfo.com/dirinfo/companies/all/10888?start=30") # Load page
companies = browser.find_elements_by_css_selector("div.title > a")
for company in companies:
    company.text // put this in Excel as the company name
    company.get_attribute("href") // put this in Excel as the company URL

companies是包含所需数据的A代码的列表。 href包含公司的网址,.text包含公司名称。