如何在浏览器中打开使用beautifulsoup访问的网页?

时间:2019-06-12 08:23:50

标签: python beautifulsoup

我需要一个脚本,该脚本将我登录到我的git hub帐户并在浏览器中打开github页面。到现在为止,它只能通过beautifulsoup获得。我需要一个打开网页并登录我的帐户。 谢谢

import requests
from bs4 import BeautifulSoup as bs
import webbrowser
from urllib.request import urlopen

login_data = {
'commit': 'Sign in',
'utf8': '✓',
'authenticity_token': 'willchange',
'login': 'maximmashkov',
'password': '12345',
'webauthn-support' : 'supported'
}

headers = {
'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'
}


with requests.Session() as s:
    url = 'https://github.com/session'
    r = s.get(url, headers=headers)
    soup = bs(r.content, 'html5lib')
    login_data['authenticity_token'] = soup.find('input', attrs = {'name': 'authenticity_token'})['value']
    response = requests.get(url)
    r = s.post(url, data=login_data, headers=headers)

1 个答案:

答案 0 :(得分:0)

首先安装selenium,然后根据您的操作系统安装chromediver,然后通过转到您保留驱动程序的文件夹并打开终端并输入chromedriver进行测试,如果没有错误,则可以使用。

之后,您需要在需要将chromedriver自己的路径作为可执行路径的地方使用此代码。

from selenium import webdriver
import time

url = "https://github.com/login"

driver = webdriver.Chrome(executable_path= 'C:/Users/reckonsys/anshuman-work/product-analysis/productanalysis/chromedriver.exe')

driver.get(url)

emailid=driver.find_element_by_id("login_field")

emailid.send_keys("put your username")

passw=driver.find_element_by_id("password")

passw.send_keys("password here")

signin=driver.find_element_by_xpath('//*[@id="login"]/form/div[3]/input[4]')

signin.click()

time.sleep(10)

# get anything here......

driver.close()