python 在 firefox 中使用 selenium 抓取网站
Crawling website using selenium in firefox with python
我写了一个简单的爬虫程序来爬取“http://fortune.com/fortune500/”,想获取<option value = "...">...</option>
的值,但是在使用webelement.text获取标签名的内容时("option"),没有显示。我想知道为什么?谁能为我解决这个问题?
# -*- coding: utf-8 -*-
import mechanize
from bs4 import BeautifulSoup
import re
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re
#-----------selenium part(ignored)----------------#
browser = webdriver.Chrome() # Get local session of firefox
browser.get("http://fortune.com/fortune500/")
time.sleep(1) # Let the page load, will be added to the API
industry_button = browser.find_element_by_name('filters[Industry]')
print industry_button
count = 0;
industry_value = industry_button.find_elements_by_tag_name('option')
for number in industry_value:
count += 1
print number
print count
.text
returns 该元素的 innerHTML
,而不是值。你想获得 value
属性。
大概是:
element.get_attribute('value')
Selenium 可以轻松处理 select->option
HTML 构造 - Select
class 提供了一个易于使用的界面。例如,.options
将列出所有可用选项。对于每个选项,您可以获取 .text
以获取内部 HTML 或 .get_attribute('value')
以获取 value
属性的值。
此外,而不是 time.sleep()
,明确等待切换按钮出现。
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
browser = webdriver.Chrome()
browser.get("http://fortune.com/fortune500/")
# toggle
toggle = WebDriverWait(browser, 10).until(EC.visibility_of_element_located((By.CLASS_NAME, "filter-toggle")))
toggle.click()
# get all options
industry_button = Select(browser.find_element_by_name("filters[Industry]"))
for option in industry_button.options:
print option.text
打印:
Industry: Any
Advertising, marketing
Aerospace and Defense
Airlines
Apparel
Automotive Retailing, Services
Beverages
...
我写了一个简单的爬虫程序来爬取“http://fortune.com/fortune500/”,想获取<option value = "...">...</option>
的值,但是在使用webelement.text获取标签名的内容时("option"),没有显示。我想知道为什么?谁能为我解决这个问题?
# -*- coding: utf-8 -*-
import mechanize
from bs4 import BeautifulSoup
import re
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re
#-----------selenium part(ignored)----------------#
browser = webdriver.Chrome() # Get local session of firefox
browser.get("http://fortune.com/fortune500/")
time.sleep(1) # Let the page load, will be added to the API
industry_button = browser.find_element_by_name('filters[Industry]')
print industry_button
count = 0;
industry_value = industry_button.find_elements_by_tag_name('option')
for number in industry_value:
count += 1
print number
print count
.text
returns 该元素的 innerHTML
,而不是值。你想获得 value
属性。
大概是:
element.get_attribute('value')
Selenium 可以轻松处理 select->option
HTML 构造 - Select
class 提供了一个易于使用的界面。例如,.options
将列出所有可用选项。对于每个选项,您可以获取 .text
以获取内部 HTML 或 .get_attribute('value')
以获取 value
属性的值。
此外,而不是 time.sleep()
,明确等待切换按钮出现。
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
browser = webdriver.Chrome()
browser.get("http://fortune.com/fortune500/")
# toggle
toggle = WebDriverWait(browser, 10).until(EC.visibility_of_element_located((By.CLASS_NAME, "filter-toggle")))
toggle.click()
# get all options
industry_button = Select(browser.find_element_by_name("filters[Industry]"))
for option in industry_button.options:
print option.text
打印:
Industry: Any
Advertising, marketing
Aerospace and Defense
Airlines
Apparel
Automotive Retailing, Services
Beverages
...