【问题标题】:web scraping w/age verification带有年龄验证的网页抓取
【发布时间】: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


【解决方案1】:

如果您的目标是点击验证进入 selenium: ps install selenium && get geckodriver(firefox) 或 chromedriver(chrome)

#Mossein~King(hi i'm here to help)
import time
import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.firefox.options import Options
from BeautifulSoup import BeautifulSoup

#this.is.for.headless.This.will.save.you.a.bunch.of.research.time(Trust.me)
options = Options()
options.add_argument("--headless")
driver = webdriver.Firefox(firefox_options=options)

#for.graphical(you.need.gecko.driver.for.firefox)
# driver = webdriver.Firefox()

url = 'your-url'
driver.get(url)

#get.the.link.to.clicking
#exaple if<a class='MosseinKing'>
driver.find_element_by_xpath("//a[@class='MosseinKing']").click()

#wait.1.secong.in.case.of.transitions
time.sleep(1)

pagesource = driver.page_source
soup = BeautifulSoup(pagesource)

#you.can.now.enjoy.soup
print soup.prettify()

【讨论】:

  • 成功安装了 selenium。 Firefox 很好,但在使用 gecko 驱动程序时遇到了问题。我似乎有驱动程序,但不确定如何在代码中调用它...运行 mac os sierra。铬呢?还有其他提示吗?
  • 只需将 geckodriver 添加到您的 PATH 中即可。检查链接。
  • 您好,谢谢,我取得了进展,并找到了元素,现在我需要让它点击。看上面已经编辑的代码
猜你喜欢
  • 1970-01-01
  • 2013-04-10
  • 2014-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多