代码主要实现功能使用selenium+chrome自动登录知乎,查找问题,并把所有的文章(作者,赞同数,内容,图片)都保存在数据库中(mongodb)
from selenium import webdriver
import time,re
import requests
from bs4 import BeautifulSoup
import pymongo
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
from gridfs import *
def selenium_test(contents):
global b
print("dssdsa")
try:
chrome_crawler="C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe"
b=webdriver.Chrome(executable_path=chrome_crawler)
b.get("https://www.zhihu.com/signin?next=%2F")
time.sleep(1)
b.find_element_by_xpath('//*[@id="root"]/div/main/div/div/div/div[3]/span[2]/button[2]').click()
time.sleep(3)
windows=b.window_handles
b.switch_to.window(windows[1])
time.sleep(4)
b.find_element_by_id('ptlogin_iframe').click()
time.sleep(2)
b.switch_to.frame('ptlogin_iframe')
id0='###'
password="###"
time.sleep(3)
b.find_element_by_id("switcher_plogin").click()
time.sleep(3)
b.find_element_by_xpath('//*[@id="u"]').send_keys