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)
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