如何在分裂或硒中创建会话?
How create session in splinter or selenium?
我正在尝试从需要登录的网站上抓取一些数据,(我尝试使用请求,但它无法处理请求)所以我使用了 splinter 并且我通过 xpath 成功登录但问题是,我想使用 beautifulsoup 抓取一些数据,所以在通过该网站的 splinter 登录后,我如何使用该会话使用 beautifulsoup 从用户帐户抓取数据。这是我的代码:
from selenium import webdriver
from splinter import Browser
web_driver=webdriver.Chrome('/Users/paul/Downloads/chromedriver/chromedriver')
url = "https://www.example.com"
browser = Browser("chrome")
visit_browser = browser.visit(url)
email_box = '//*[@id="email"]'
find_1 = browser.find_by_xpath(email_box)
find_1.fill("example@gmail.com")
password_box = '//*[@id="pass"]'
find_2 = browser.find_by_xpath(password_box)
find_2.fill("example12345")
button_sub = '//*[@id="u_0_5"]'
find_3 = browser.find_by_xpath(button_sub)
find_3.click()
"""i tried like this with Beautifulsoup but not working , its giving login page instead of "after login page" """
get_url=requests.get(url)
soup=BeautifulSoup(get_url.text,"html.parser")
print(soup.text)
我成功登录了,假设现在我在登录后出现的首页,如何保存这个会话并在这个会话中工作,并使用漂亮的汤来抓取数据和打印。
Beautiful Soup 不支持从 Splinter/Selenium 转移会话,最好继续使用 splinter,并在登录过程完成后从 splinter 中抓取文本。
您导入了碎片。您可以使用它来使用 BeautifulSoup 打开和操作页面 — 无需请求 — 就像这样。
需要注意的关键事实是 page = browser.html
行为您提供了页面的 HTML 内容供 BeautifulSoup 解析。
>>> from splinter import Browser
>>> browser = Browser('firefox')
>>> browser.visit('https://ville.montreal.qc.ca/')
>>> page = browser.html
>>> import bs4
>>> soup = bs4.BeautifulSoup(page, 'lxml')
>>> soup.find_all(string='Liens rapides')
['Liens rapides']
(顺便说一句,我使用的是 Firefox 浏览器,因为我使用的计算机不支持 Chrome。)
您还导入了硒。因此,您可以 或者 使用它打开和操作页面,甚至可以将页面副本提供给 BeautifulSoup 进行解析。
在这种情况下,重要的是要注意使用 selenium 下载的页面内容可用于 BeautifulSoup 作为 driver.page_source
。
>>> from selenium import webdriver
>>> driver = webdriver.Chrome()
>>> driver.get('https://ville.montreal.qc.ca/')
>>> links = driver.find_elements_by_xpath('.//a[contains(text(),"English")]')
>>> links
[<selenium.webdriver.remote.webelement.WebElement (session="abd3226550e94776152c619b509dd158", element="0.34892531934022464-1")>]
>>> import bs4
>>> soup = bs4.BeautifulSoup(driver.page_source, 'lxml')
>>> soup.find_all(string='Liens rapides')
['Liens rapides']
我正在尝试从需要登录的网站上抓取一些数据,(我尝试使用请求,但它无法处理请求)所以我使用了 splinter 并且我通过 xpath 成功登录但问题是,我想使用 beautifulsoup 抓取一些数据,所以在通过该网站的 splinter 登录后,我如何使用该会话使用 beautifulsoup 从用户帐户抓取数据。这是我的代码:
from selenium import webdriver
from splinter import Browser
web_driver=webdriver.Chrome('/Users/paul/Downloads/chromedriver/chromedriver')
url = "https://www.example.com"
browser = Browser("chrome")
visit_browser = browser.visit(url)
email_box = '//*[@id="email"]'
find_1 = browser.find_by_xpath(email_box)
find_1.fill("example@gmail.com")
password_box = '//*[@id="pass"]'
find_2 = browser.find_by_xpath(password_box)
find_2.fill("example12345")
button_sub = '//*[@id="u_0_5"]'
find_3 = browser.find_by_xpath(button_sub)
find_3.click()
"""i tried like this with Beautifulsoup but not working , its giving login page instead of "after login page" """
get_url=requests.get(url)
soup=BeautifulSoup(get_url.text,"html.parser")
print(soup.text)
我成功登录了,假设现在我在登录后出现的首页,如何保存这个会话并在这个会话中工作,并使用漂亮的汤来抓取数据和打印。
Beautiful Soup 不支持从 Splinter/Selenium 转移会话,最好继续使用 splinter,并在登录过程完成后从 splinter 中抓取文本。
您导入了碎片。您可以使用它来使用 BeautifulSoup 打开和操作页面 — 无需请求 — 就像这样。
需要注意的关键事实是 page = browser.html
行为您提供了页面的 HTML 内容供 BeautifulSoup 解析。
>>> from splinter import Browser
>>> browser = Browser('firefox')
>>> browser.visit('https://ville.montreal.qc.ca/')
>>> page = browser.html
>>> import bs4
>>> soup = bs4.BeautifulSoup(page, 'lxml')
>>> soup.find_all(string='Liens rapides')
['Liens rapides']
(顺便说一句,我使用的是 Firefox 浏览器,因为我使用的计算机不支持 Chrome。)
您还导入了硒。因此,您可以 或者 使用它打开和操作页面,甚至可以将页面副本提供给 BeautifulSoup 进行解析。
在这种情况下,重要的是要注意使用 selenium 下载的页面内容可用于 BeautifulSoup 作为 driver.page_source
。
>>> from selenium import webdriver
>>> driver = webdriver.Chrome()
>>> driver.get('https://ville.montreal.qc.ca/')
>>> links = driver.find_elements_by_xpath('.//a[contains(text(),"English")]')
>>> links
[<selenium.webdriver.remote.webelement.WebElement (session="abd3226550e94776152c619b509dd158", element="0.34892531934022464-1")>]
>>> import bs4
>>> soup = bs4.BeautifulSoup(driver.page_source, 'lxml')
>>> soup.find_all(string='Liens rapides')
['Liens rapides']