Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python通过webscraping登录Google?_Python_Selenium_Web Scraping_Beautifulsoup_Google Login - Fatal编程技术网

如何使用python通过webscraping登录Google?

如何使用python通过webscraping登录Google?,python,selenium,web-scraping,beautifulsoup,google-login,Python,Selenium,Web Scraping,Beautifulsoup,Google Login,我一直试图通过使用python(selium和beutifulsoup)在网页上涂鸦来进入google网页,但这需要登录。问题是我发出post请求来输入我的电子邮件,但它不会更改网页: with requests.Session() as s: p = s.post(new_url, data={ "email": 'my_mail_here', }) new_url = driver.current_u

我一直试图通过使用python(selium和beutifulsoup)在网页上涂鸦来进入google网页,但这需要登录。问题是我发出post请求来输入我的电子邮件,但它不会更改网页:

with requests.Session() as s:
        p = s.post(new_url, data={
            "email": 'my_mail_here',
        })
        new_url = driver.current_url
        driver.get(new_url)
        time.sleep(3)
        p = s.post(new_url, data={
            "password": "my_pass_here"
        })
        new_url = driver.current_url
        driver.get(new_url)
        time.sleep(3)
        base_page = s.get(url)
        soup = BeautifulSoup(base_page.content, 'html.parser')
        print(soup.prettify())

使用Selenium,您可以使用send_键。安装selenium,安装浏览器驱动程序,为selenium提供浏览器和驱动程序的位置。导航到该网站。检查输入框中的用户名和密码。使用xpath、class、id查找适合您需要的元素。使用下面的发送键。这是Mac的粗略设置。Windows略有不同

from selenium import webdriver
import time
from random import randint as rnd

options = webdriver.ChromeOptions()
options.binary_location = '/Users/user/Desktop/Google Chrome.app/Contents/MacOS/Google Chrome'
chrome_driver_exec = '/Users/user/Documents/WebDrivers/chromedriver-3'
driver = webdriver.Chrome(chrome_driver_exec, options=options)

driver.get('https://website.com')
driver.set_window_size(1440, 900)
time.sleep(rnd(3, 5))
# login
driver.find_element_by_xpath('/html/body/div[1]/div[5]/div[1]/div[2]/div/div[2]/ul/li[5]/a').click()
time.sleep(rnd(3, 5))

driver.find_element_by_xpath('/html/body/div[1]/div[2]/div[1]/div/div[3]/div[2]/form/div[2]/div[1]/input').send_keys('username')
time.sleep(rnd(2, 4))
driver.find_element_by_xpath('/html/body/div[1]/div[2]/div[1]/div/div[3]/div[2]/form/div[2]/div[2]/div/div/input').send_keys('password')