Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 能够浏览静态而非动态网站_Python_Selenium_Selenium Webdriver_Web Scraping - Fatal编程技术网

Python 能够浏览静态而非动态网站

Python 能够浏览静态而非动态网站,python,selenium,selenium-webdriver,web-scraping,Python,Selenium,Selenium Webdriver,Web Scraping,我正试图从ESPN上抓取下一场比赛的时间,你可以在ESPN上找到:(现在看起来是尤文图斯和AC米兰之间的足球比赛) 我的webscrape有以下python代码: 导入请求 从lxml导入html 从selenium导入webdriver 导入chromedriver\u二进制文件 driver=webdriver.Chrome() 司机,上车https://www.espn.com/') tree=html.fromstring(driver.page\u源) time=tree.xpath(

我正试图从ESPN上抓取下一场比赛的时间,你可以在ESPN上找到:(现在看起来是尤文图斯和AC米兰之间的足球比赛)

我的webscrape有以下python代码:

导入请求
从lxml导入html
从selenium导入webdriver
导入chromedriver\u二进制文件
driver=webdriver.Chrome()
司机,上车https://www.espn.com/')
tree=html.fromstring(driver.page\u源)
time=tree.xpath('/*[@id=“news feed”]/section[1]/header/a/div[2]/span[2]/span')
打印(时间)
但它返回以下错误:

Traceback (most recent call last):
  File "c:\Users\akash\Coding\test\scrape.py", line 9, in <module>
    tree = html.fromstring(driver.page_source)
  File "C:\Users\akash\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 679, in page_source
    return self.execute(Command.GET_PAGE_SOURCE)['value']
  File "C:\Users\akash\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\akash\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchWindowException: Message: no such window: target window already closed
from unknown error: web view not found
  (Session info: chrome=83.0.4103.97)
回溯(最近一次呼叫最后一次):
文件“c:\Users\akash\Coding\test\scrape.py”,第9行,在
tree=html.fromstring(driver.page\u源)
文件“C:\Users\akash\AppData\Local\Programs\Python\Python38-32\lib\site packages\selenium\webdriver\remote\webdriver.py”,第679行,在第页\u source
返回self.execute(Command.GET_PAGE_SOURCE)['value']
文件“C:\Users\akash\AppData\Local\Programs\Python\Python38-32\lib\site packages\selenium\webdriver\remote\webdriver.py”,第321行,在execute中
self.error\u handler.check\u响应(响应)
文件“C:\Users\akash\AppData\Local\Programs\Python\Python38-32\lib\site packages\selenium\webdriver\remote\errorhandler.py”,第242行,在check\u响应中
引发异常类(消息、屏幕、堆栈跟踪)
selenium.common.exceptions.NoSuchWindowException:消息:无此类窗口:目标窗口已关闭
来自未知错误:找不到web视图
(会话信息:chrome=83.0.4103.97)
我怀疑问题是因为这是ESPN网站上的动态内容,因为我能够使用相同的代码(除了更改URL和XPath之外)从另一个网站上获取具有常量数据的数据。有人能帮忙修复这个错误吗

我已经在代码中安装了每个python库。
(注意:我已经看过了)在我的例子中,我使用了从下载的二进制文件。代码如下:

from lxml import html
from selenium import webdriver
from selenium.webdriver.chrome.options import Options  
chrome_options = Options()  
chrome_options.add_argument("--headless") 

driver = webdriver.Chrome(r'./chromedriver', chrome_options=chrome_options)
driver.get('https://www.espn.com/')
tree = html.fromstring(driver.page_source)
time = tree.xpath("//*[@id='news-feed']//span[@class='game-time']/text()")[0].strip()
print(time)

其中传递给
chrome\u options
--headless
参数是可选的(这只是在“headless模式”下运行chrome)。

您好,谢谢您的回答。不幸的是,我仍然有一个问题:(为“测试”这件事的奇怪交叉表示抱歉),我使用了与您完全相同的代码。我的
chromedriver.exe
位于项目文件夹中的
chromedriver
文件夹中。您是否编写了文件的扩展名“.exe”?这样:driver=webdriver.Chrome(r./chromedriver.exe',Chrome\u options=Chrome\u options)好的,所以看到你的评论后,我把它改为
driver=webdriver.Chrome(r./chromedriver/chromedriver.exe',options=Chrome\u options)
,但现在只输出
[]
(因为即将到来的游戏已经不存在了,所以我改成了ESPN以外的内容)。这是动态网站的另一个问题吗?我也尝试过从另一个网站抓取,效果很好。这也没有问题。您要解析的表数据在iframe中,所以首先您应该切换到iframe:
driver.switch\u to.frame(“cdcCharts3”)
然后查找所有标题:
table_headers=driver。通过xpath('/*[@id=“cdc-chart-1-data”]/thead/tr/th')查找元素。
最后获取标题文本,如:
table_headers_text=[h.get_属性('textContent'),用于表_标题中的h]
然后你得到一个标题数据数组。然后你可以得到所有的表行数据,但不需要再次切换到iframe,因为你已经在。但是如果你想解析另一个表数据,那么你应该切换到另一个frame1。有很多方法可以做到这一点,但这是另一个讨论的主题。2.所有网站的标记都不同,所以一般的方法会奏效,但你需要单独处理。请将我的答案标记为有帮助的,并向上投票我的评论,因为他们回答了你所有的问题。你到底想从网站上刮些什么?因为我在两天后看了这篇文章,显然内容已经改变了,所以我不知道你想要什么由于内容发生了变化,我一直在尝试从另一个网站上抓取。查看下面答案上的评论,查看我与Dmitry的讨论。