- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试用 python 编写一个网络抓取,它将激活网页上某些按钮的“onclick”功能,因为包含我想要的数据的表格被转换为 csv,这使得访问变得更容易。但问题是我在使用 PhantomJs 时根本无法通过 xpath 定位元素。如何单击该元素并访问我想要的 csv 内容?
这是我的代码:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.common.proxy import *
url = "http://www.pro-football-reference.com/boxscores/201609180nwe.htm"
xpath = "//*[@id='all_player_offense']/div[1]/div/ul/li[1]/div/ul/li[3]/button"
path_to_phantomjs = 'browser/phantomjs'
browser = webdriver.PhantomJS(executable_path = path_to_phantomjs)
browser.get(url)
delay=3
element_present = EC.presence_of_element_located((By.ID, 'all_player_offense'))
WebDriverWait(browser, delay).until(element_present)
browser.find_element_by_xpath(xpath).click()
我收到此错误:
selenium.common.exceptions.NoSuchElementException: Message: {"errorMessage":"Unable to find element with xpath '//*[@id='all_player_offense']/div[1]/div/ul/li[1]/div/ul/li[3]/button'","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"153","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:50989","User-Agent":"Python-urllib/2.7"},"httpVersion":"1.1","method":"POST","post":"{\"using\": \"xpath\", \"sessionId\": \"93ff24f0-9cbe-11e6-8711-bdfa3ff9cfb1\", \"value\": \"//*[@id='all_player_offense']/div[1]/div/ul/li[1]/div/ul/li[3]/button\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/93ff24f0-9cbe-11e6-8711-bdfa3ff9cfb1/element"}}
Screenshot: available via screen
最佳答案
IMPORTANT THING I FORGOT TO MENTION: As described in this this issue on GitHub, try putting
set_window_size(width, height)
ormaximize_window()
after setting the webdriver. You should also consider telling the webdriver toimplicitly_wait(10)
for the element to appear.
因此,为了让 Selenium Webdriver 正确模拟您正在做的事情,您必须执行一个特殊的操作。本质上,要获得所需的数据,您必须:
A:将鼠标悬停在“共享及更多”下拉菜单上。然后
B:点击“以 CSV (Excel) 形式获取表格”。
对于 A,这涉及必须将模拟光标放在元素上而不单击它。这种“鼠标悬停”的想法可以通过 ActionChains
类中提供的 move_to_element()
函数来实现。因此,您可以在顶部插入以下内容:
from selenium.webdriver.common.action_chains import ActionChains
您希望 Selenium 找到特定元素并移动到它。您可以通过两行代码实现此目的:
dropdown = browser.find_element_by_xpath('//*[@id="all_player_offense"]/div[1]/div/ul/li[1]')
ActionChains(browser).move_to_element(dropdown).perform()
如果省略上述内容,您将收到 ElementNotVisibleException
。
现在对于B,您应该能够执行browser.find_element_by_xpath(xpath).click()
。
关于Python与selenium webscraping无法找到元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40297647/
我正在尝试使用 R 抓取 OECD 表格。 library(XML) OECD <- readHTMLTable('http://stats.oecd.org/Index.aspx?DataSetCo
我正在使用anaconda并试图创建一个可以在Linkedin上继续使用并从页面中刮取相关信息的刮刀。目前,我只是想让它登录并从相关页面中提取源代码。但是,下面的代码不断返回“TypeError:'N
我正在创建一个 webscraping 函数来使用数据生成 json,webscraper 部分可以工作,奇怪的是该函数返回未定义 获取产品.js module.exports.getproduct
task :fetch_front => :environment do require 'rubygems' require 'nokogiri' require 'open-uri' requi
我为 realtor.com 构建了一个 webscraper,因为我正在寻找我所在地区的房屋和代理商,这对我来说很容易,但是他们只是更改了他们网站上的代码(可能是为了阻止人们这样做),现在我是得到一
我是 Python 新手,但对 R 很了解。我正在尝试从雅虎网络抓取股票价格数据。我成功检索了价格数据并能够创建数据框。然而,雅虎包括了何时支付股息。目前,我想忽略股息,但我在过滤数据框以在支付股息时
我希望能够将来自多个 New Egg 网页的数据抓取到一个 csv 文件中。 我目前能够将单个 New Egg 网页中的数据网络抓取到一个 csv 文件中,但是我希望同时对多个页面进行网络抓取。 fr
我正在使用 DryScrape 抓取一个 javascript 页面,如果出现错误,它偶尔会终止进程。我已经尝试根据文档使用 catch 来防止它,但我还没有弄明白: try:
背景 大家好...python 和网络抓取的新手。我在 Mac (Sierra) 上,在 Firefox (87.0) 中运行 Jupyter Notebook。我正在尝试从这样的网页中抓取多个值:h
我无法解决 R 中的这个问题,如果您能在这里给我一些建议,我将不胜感激。 我正在尝试从 https://www.investing.com/rates-bonds/spain-5-year-bond-
对于一个简单的网络抓取工具,我遇到了以下代码的 unicode 编码错误。 print 'JSON scraper initializing' from bs4 import BeautifulSou
我正在尝试从 this website 抓取实时出发表使用 BeautifulSoup 。 我尝试过以下方法: caremar_live_departures_table = list(soup.se
目前的代码下载并打印到屏幕上,但我如何将打印的 Material 放入 sql 数据库中。如果我想将数据放入 CSV 文件中,Python(在美好的一天)似乎会创建该文件automatically.O
当我尝试通过 python 使用 Selenium 库对网站进行网络抓取时遇到问题。关键是我想获得一些有关收集到该站点的歌曲的信息:https://bandcamp.com/?g=all&s=top&
您好,我正在使用 Beautifulsoup 解析网站并获取名称作为输出。但是在运行脚本之后,我得到了一个 [u'word1', u'word2', u'word3'] 输出。我正在寻找的是 'wor
我是网络抓取工具的新手。我做了一些有用的东西,但要花好几个小时才能得到我需要的一切。我阅读了一些关于使用并行进程来处理 URL 的内容,但我不知道如何去做并将其合并到我已有的内容中。非常感谢您的帮助!
我正在尝试制作一个简单的nodeJS webscraper,但我不知道如何在 Json 文件中格式化我的结果。 我正在使用express、request、cheerio和fs。 这是我的代码: var
Scraper 的目标是分析哪些棋盘游戏获得最多点赞并将其打印在排序列表中。基本上是一本“名字:竖起大拇指”的字典。以下是我要排序的游戏列表: https://boardgamegeek.com/ge
我正在尝试从网站上抓取广告的 URL 链接。我在 jupyter 笔记本中有以下代码 from bs4 import BeautifulSoup import requests link = "htt
我正在尝试抓取一个房地产网站的列表。它有一个 aspx 表单,必须在提交前填写。 http://www.cbre.us/PropertyListings/Pages/Properties-for-Sa
我是一名优秀的程序员,十分优秀!