How to get data off from a web page in selenium webdriver

非 Y 不嫁゛ 提交于 2019-11-28 12:58:49

问题


I want to fetch company name, email, phone number from this Link and put these contents in an excel file. I want to do the same for the all pages of the website. I have got the logic to fetch the the links in the browser and switch in between them. I'm unable to fetch the data from the website. Can anybody provide me an enhancement to the code i have written.

Below is the code i have written:

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import time
from lxml import html
import requests
import xlwt

browser = webdriver.Firefox() # Get local session of firefox

# 0 wait until the pages are loaded
browser.implicitly_wait(3) # 3 secs should be enough. if not, increase it

browser.get("http://ae.bizdirlib.com/taxonomy/term/1493") # Load page
links = browser.find_elements_by_css_selector("h2 > a")

#print link
for link in links:
    link.send_keys(Keys.CONTROL + Keys.RETURN)
    link.send_keys(Keys.CONTROL + Keys.PAGE_UP)
#tree = html.fromstring(link.text)
    time.sleep(5)

回答1:


What have you tried to fetch data from those links? I can show you an example. If you want to get "S.S.D Middle East - F.Z.E", after you click on its link, it has a number of attributes ranging from Company name to Industry. If you want to locate and retrieve its company name, you need to locate it first and get its text:

companyNameElement = browser.find_element_by_css_selector("div[class="region region-content"] span[itemprop="name"]");
companyName = companyNameElement.getText()

You should get "S.S.D Middle East - F.Z.E" in this companyName variable.



来源:https://stackoverflow.com/questions/32858189/how-to-get-data-off-from-a-web-page-in-selenium-webdriver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!