0 python click web-scraping selenium-webdriver
作为一名初级程序员,我在这个网站上找到了很多有用的信息,但找不到我的具体问题的答案。我想从网页中抓取数据,但是我有兴趣抓取的一些数据只能在单击“更多”按钮后才能获得。下面的代码执行时不会产生错误,但它似乎没有单击“更多”按钮并在页面上显示附加数据。我只对查看“成绩单”选项卡上的信息感兴趣,这对我来说似乎有点复杂,因为其他选项卡上有“更多”按钮。我的代码的相关部分如下:
from mechanize import Browser
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver import ActionChains
import urllib2
import mechanize
import logging
import time
import httplib
import os
import selenium
url="http://seekingalpha.com/symbol/IBM/transcripts"
ua='Mozilla/5.0 (X11; Linux x86_64; rv:18.0) Gecko/20100101 Firefox/18.0 (compatible;)'
br=Browser()
br.addheaders=[('User-Agent', ua), ('Accept', '*/*')]
br.set_debug_http(True)
br.set_debug_responses(True)
logging.getLogger('mechanize').setLevel(logging.DEBUG)
br.set_handle_robots(False)
chromedriver="~/chromedriver"
os.environ["webdriver.chrome.driver"]=chromedriver
driver=webdriver.Chrome(chromedriver)
time.sleep(1)
httplib.HTTPConnection._http_vsn=10
httplib.HTTPConnection._http_vsn_str='HTTP/1.0'
page=br.open(url)
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(5)
actions=ActionChains(driver)
elem=driver.find_element_by_css_selector("div #transcripts_show_more div#more.older_archives")
actions.move_to_element(elem).click()
Run Code Online (Sandbox Code Playgroud)
几件事:
鉴于你使用selenium
,你不需要任何mechanize
或urllib2
作为selenium
做实际的页面加载。至于其他的进口(httplib
,logging
,os
和time
),他们要么未使用或冗余。
为方便起见,我将代码更改为使用Firefox
; 您可以将其改回Chrome
(或其他任何浏览器)。
关于ActionChains
,您不要在这里使用它们,因为您只需单击一下(实际上没有任何链接)。
鉴于浏览器正在接收数据(通过 AJAX)而不是加载新页面,我们不知道新数据何时出现;所以我们需要检测变化。
我们知道“单击”按钮会加载更多<li>
标签,因此我们可以检查<li>
标签数量是否发生了变化。这就是这一行的作用:
WebDriverWait(selenium_browser, 10).until(lambda driver: len(driver.find_elements_by_xpath("//div[@id='headlines_transcripts']//li")) != old_count)
Run Code Online (Sandbox Code Playgroud)
它将等待最多 10 秒,定期比较<li>
按钮单击之前和期间的当前标签数量。
import selenium
from selenium import webdriver
from selenium.common.exceptions import StaleElementReferenceException
from selenium.common.exceptions import WebDriverException
from selenium.common.exceptions import TimeoutException as SeleniumTimeoutException
from selenium.webdriver.support.ui import WebDriverWait
url = "http://seekingalpha.com/symbol/IBM/transcripts"
selenium_browser = webdriver.Firefox()
selenium_browser.set_page_load_timeout(30)
selenium_browser.get(url)
selenium_browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")
elem = selenium_browser.find_element_by_css_selector("div #transcripts_show_more div#more.older_archives")
old_count = len(selenium_browser.find_elements_by_xpath("//div[@id='headlines_transcripts']//li"))
elem.click()
try:
WebDriverWait(selenium_browser, 10).until(lambda driver: len(driver.find_elements_by_xpath("//div[@id='headlines_transcripts']//li")) != old_count)
except StaleElementReferenceException:
pass
except SeleniumTimeoutException:
pass
print(selenium_browser.page_source.encode("ascii", "ignore"))
Run Code Online (Sandbox Code Playgroud)
我在 python2.7 上;如果你使用的是 python3.X,你可能不需要.encode("ascii", "ignore")
.
归档时间: |
|
查看次数: |
1083 次 |
最近记录: |