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 Selenium登录看起来很有效,但随后BeautifulSoup输出显示登录页面_Python_Selenium_Beautifulsoup - Fatal编程技术网

Python Selenium登录看起来很有效,但随后BeautifulSoup输出显示登录页面

Python Selenium登录看起来很有效,但随后BeautifulSoup输出显示登录页面,python,selenium,beautifulsoup,Python,Selenium,Beautifulsoup,我正试图用Python编写一个脚本来获取我梦幻足球联盟的所有名单,但你必须先登录ESPN。下面是我的代码。当它运行时,它看起来好像在工作——也就是说,我看到登录页面出现,我看到它登录,页面关闭。然后当我打印汤时,我看不到任何球队名单。我将soup输出保存为一个html文件,以查看它是什么,它只是一个页面,用于重定向我再次登录。在尝试登录之前,是否通过BS4加载页面 import time from selenium import webdriver from selenium.common.ex

我正试图用Python编写一个脚本来获取我梦幻足球联盟的所有名单,但你必须先登录ESPN。下面是我的代码。当它运行时,它看起来好像在工作——也就是说,我看到登录页面出现,我看到它登录,页面关闭。然后当我打印汤时,我看不到任何球队名单。我将soup输出保存为一个html文件,以查看它是什么,它只是一个页面,用于重定向我再次登录。在尝试登录之前,是否通过BS4加载页面

import time
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import urllib.request as urllib2
from bs4 import BeautifulSoup

driver = webdriver.Chrome()

driver.get("http://games.espn.go.com/ffl/signin")
#implement wait it is mandatory in this case
WebDriverWait(driver,1000).until(EC.presence_of_all_elements_located((By.XPATH,"(//iframe)")))
frms = driver.find_elements_by_xpath("(//iframe)")

driver.switch_to_frame(frms[2])
time.sleep(2)
driver.find_element_by_xpath("(//input)[1]").send_keys("userrname")
driver.find_element_by_xpath("(//input)[2]").send_keys("password")
driver.find_element_by_xpath("//button").click()
driver.switch_to_default_content()
time.sleep(4)
#driver.close()

# specify the url
roster_page = 'http://games.espn.com/ffl/leaguerosters?leagueId=11111'
# query the website and return the html to the variable 'page'
page = urllib2.urlopen(roster_page)
# parse the html using beautiful soup and store in variable `soup`
soup = BeautifulSoup(page, 'html.parser')

您使用selenium登录,然后使用urllib2打开URL,该URL使用另一个会话转到站点。从selenium webdriver获取源代码,然后将其与BeautifulSoup一起使用,应该可以使用。

您使用selenium登录,然后使用urllib2打开URL,该URL使用另一个会话转到站点。从selenium webdriver获取源代码,然后将其与BeautifulSoup一起使用,它应该可以工作。

尝试使用此方法而不是urllib2

driver.get("http://games.espn.com/ffl/leaguerosters?leagueId=11111")
# query the website and return the html to the variable 'page'
page = driver.page_source
# parse the html using beautiful soup and store in variable 'soup'
soup = BeautifulSoup(page, 'html.parser')

试试这个,而不是urllib2

driver.get("http://games.espn.com/ffl/leaguerosters?leagueId=11111")
# query the website and return the html to the variable 'page'
page = driver.page_source
# parse the html using beautiful soup and store in variable 'soup'
soup = BeautifulSoup(page, 'html.parser')

您在浏览器中通过Selenium执行的请求与您通过
urllib
发出的请求没有任何共同之处。只需将用户名/密码传递到HTTP请求,以授权用户的身份请求数据(不需要Selenium)或使用pure Selenium作业(请注意,Selenium有足够的内置方法来抓取页面),更具体地说,
cookies
不在Selenium和urllib2之间共享,因此当您使用urllib2发出请求时,Web服务器将无法检测到您以前的登录。正如其他人所说的,只要在所有HTTP请求中使用Selenium,您就应该可以了。@Ionut Ticus:您似乎说到点子上了——我这里也有同样的问题:您知道如何修复它吗!?期待您的来信。您在浏览器中通过Selenium执行的regardsRequests与您通过
urllib
发出的请求没有任何共同之处。只需将用户名/密码传递到HTTP请求,以授权用户的身份请求数据(不需要Selenium)或使用pure Selenium作业(请注意,Selenium有足够的内置方法来抓取页面),更具体地说,
cookies
不在Selenium和urllib2之间共享,因此当您使用urllib2发出请求时,Web服务器将无法检测到您以前的登录。正如其他人所说的,只要在所有HTTP请求中使用Selenium,您就应该可以了。@Ionut Ticus:您似乎说到点子上了——我这里也有同样的问题:您知道如何修复它吗!?期待您的来信。晚上好亲爱的Dakshinarmurty Karra:非常感谢你用这个伟大的答案来回答这个问题:我遇到了完全相同的问题:–cf-我想我必须深入挖掘你的想法和方法。-期待您的来信。问候零晚上好亲爱的Dakshinarmurty Karra:非常感谢你用这个伟大的答案来回答这个问题:我遇到了完全相同的问题:–cf-我想我必须深入挖掘你的想法和方法。-期待您的来信。关于零