简体   繁体   English

如何从需要登录的网站上抓取信息

[英]How to scrape information from website that requires login

I am working on a python web scraping project.我正在研究 python web 抓取项目。 The website I am trying to scrape data from contains info about all the medicines sold in India.我试图从中抓取数据的网站包含有关在印度销售的所有药品的信息。 The website requires a user to login before giving access to this information.该网站要求用户登录才能访问此信息。

I want to access all the links in this url https://mims.com/india/browse/alphabet/a?cat=drug&tab=brand and store it in an array.我想访问此 url https://mims.com/india/browse/alphabet/a?cat=drug&tab=brand中的所有链接并将其存储在数组中。

Here is my code for logging into the website这是我登录网站的代码

##################################### Method 1
import mechanize
import http.cookiejar as cookielib
from bs4 import BeautifulSoup
import html2text

br = mechanize.Browser()
cj = cookielib.LWPCookieJar()

br.set_cookiejar(cj)

br.set_handle_equiv(True)
br.set_handle_gzip(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)

br.addheaders = [('User-agent', 'Chrome')]

br.open('https://sso.mims.com/Account/SignIn')

# View available forms
for f in br.forms():
    print(f)

br.select_form(nr=0)

# User credentials
br.form['EmailAddress'] = <USERNAME>
br.form['Password'] = <PASSWORD>

# Login
br.submit()
print(br.open('https://mims.com/india/browse/alphabet/a?cat=drug&tab=brand').read())

But the problem is that when the credentials are submitted, a middle page pops up with the following information.但问题是,当提交凭证时,会弹出一个中间页面,其中包含以下信息。

You will be redirected to your destination shortly.

This page submits a hidden form and only then is the required end page shown.此页面提交隐藏表单,然后才显示所需的结束页面。 I want to access the end page.我想访问结束页面。 But br.open('https://mims.com/india/browse/alphabet/a?cat=drug&tab=brand').read() accesses the middle page and prints the results.但是br.open('https://mims.com/india/browse/alphabet/a?cat=drug&tab=brand').read()访问中间页面并打印结果。

How do I wait for the middle page to submit the hidden form and then access the contents of the end page?如何等待中间页面提交隐藏表单,然后访问结束页面的内容?

I've posted a selenium solution below, which works, but after understanding a bit more about the login process, it's possible to login using BeautifulSoup and requests only.我在下面发布了一个selenium解决方案,该解决方案有效,但在了解更多有关登录过程后,可以使用BeautifulSoup登录并仅requests Please read the comments on the code.请阅读代码上的注释。

BeautifulSoup / requests solution BeautifulSoup / 请求解决方案

import requests
from bs4 import BeautifulSoup

d = {
    "EmailAddress": "your@email.tld",
    "Password": "password",
    "RememberMe":   True,
    "SubscriberId": "",
    "LicenseNumber":    "",
    "CountryCode":  "SG"
}

req = requests.Session()

login_u = "https://sso.mims.com/"
html = req.post(login_u, data=d)

products_url = "https://mims.com/india/browse/alphabet/a?cat=drug"
html = req.get(products_url) # The cookies generated on the previous request will be use on this one automatically because we use Sessions

# Here's the tricky part. The site uses 2 intermediary "relogin" pages that (theoretically) are only available with JavaScript enabled, but we can bypass that, i.e.:

soup = BeautifulSoup(html.text, "html.parser")
form = soup.find('form', {"id": "openid_message"})
form_url = form['action'] # used on the next post request

inputs = form.find_all('input')
form_dict = {}
for input in inputs:
    if input.get('name'):
        form_dict[input.get('name')] = input.get('value')

form_dict['submit_button'] = "Continue"
relogin = req.post(form_url, data=form_dict)


soup = BeautifulSoup(relogin.text, "html.parser")
form = soup.find('form', {"id": "openid_message"})
form_url = form['action'] # used
inputs = form.find_all('input')
form_dict = {}
for input in inputs:
    if input.get('name'):
        form_dict[input.get('name')] = input.get('value')
products_a = req.post(form_url, data=form_dict)
print(products_a.text)

# You can now request any url normally because the necessary cookies are already present on the current Session()
products_url = "https://mims.com/india/browse/alphabet/c?cat=drug"
products_c = req.get(products_url)
print(products_c.text)

Selenium solution Selenium解决方案

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from time import sleep

driver = webdriver.Firefox()
wait = WebDriverWait(driver, 10)
driver.maximize_window()

driver.get("https://sso.mims.com/")
el = wait.until(EC.element_to_be_clickable((By.ID, "EmailAddress")))
el.send_keys("your@email.com")

el = wait.until(EC.element_to_be_clickable((By.ID, "Password")))
el.send_keys("password")

el = wait.until(EC.element_to_be_clickable((By.ID, "btnSubmit")))
el.click()

wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "profile-section-header"))) # we logged in successfully

driver.get("http://mims.com/india/browse/alphabet/a?cat=drug")
wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "searchicon")))
print(driver.page_source)

# do what you need with the source code

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM