【发布时间】:2018-07-07 09:08:33
【问题描述】:
您好,我想使用 python 3.x 和 beautifulsoup 从一个带有年龄验证弹出窗口的网站抓取数据。如果不单击“是”来表示“您是否已超过 21 岁”,我将无法访问基础文本和图像。感谢您的支持。
编辑:谢谢,在评论的帮助下,我看到我可以使用 cookie,但不确定如何使用请求包管理/存储/调用 cookie。
因此,在另一个用户的帮助下,我正在使用 selenium 包,这样它也可以在它是图形覆盖的情况下工作(我认为?)。很难让它与壁虎驱动程序一起工作,但会继续努力!再次感谢大家的建议。
编辑 3:好的,我已经取得了进展,我可以使用 gecko 驱动程序打开浏览器窗口!~ 不幸的是它不喜欢那个链接规范,所以我再次发布。在年龄验证上单击“是”的链接被隐藏在该页面上,称为 mlink...
编辑 4:取得了一些进展,更新的代码如下。我设法在 XML 代码中找到了元素,现在我只需要设法点击链接。
#
import time
import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from bs4 import BeautifulSoup
driver = webdriver.Firefox(executable_path=r'/Users/jeff/Documents/geckodriver') # Optional argument, if not specified will search path.
driver.get('https://www.shopharborside.com/oakland/#/shop/412');
url = 'https://www.shopharborside.com/oakland/#/shop/412'
driver.get(url)
#
driver.find_element_by_class_name('hhc_modal-body').click(Yes)
#wait.1.second
time.sleep(1)
pagesource = driver.page_source
soup = BeautifulSoup(pagesource)
#you.can.now.enjoy.soup
print(soup.prettify())
Edit new:又卡住了,这里是当前代码。我似乎已经隔离了元素“mBtnYes”,但在运行代码时出现错误: ElementClickInterceptedException:消息:元素在点 (625,278.5500030517578) 处不可点击,因为另一个元素遮住了它
import time
import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from bs4 import BeautifulSoup
driver = webdriver.Firefox(executable_path=r'/Users/jeff/Documents/geckodriver') # Optional argument, if not specified will search path.
driver.get('https://www.shopharborside.com/oakland/#/shop/412');
url = 'https://www.shopharborside.com/oakland/#/shop/412'
driver.get(url)
#
driver.find_element_by_id('myBtnYes').click()
#wait.1.second
time.sleep(1)
pagesource = driver.page_source
soup = BeautifulSoup(pagesource)
#you.can.now.enjoy.soup
print(soup.prettify())
【问题讨论】:
-
所以...然后,您需要使用 Python 单击该按钮。找到负责的表单,指定参数并发送!
-
您可以在您的请求中使用此 cookie
document.cookie = "ageConfirmation=true;";ageConfirmation=true,因为网站正在检查它以显示年龄确认检查
标签: python python-3.x beautifulsoup