from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from lxml import etree
import time
driver = webdriver.Chrome()
driver.implicitly_wait(5)
chrome_option = webdriver.ChromeOptions()
chrome_option.add_argument('--proxy--server=127.0.0.1:8080')#使用代理IP,告诉服务器这是人为操作
driver.get('https://www.zhihu.com/')
driver.implicitly_wait(6)
driver.find_element_by_xpath('//*[@id="root"]/div/main/div/div/div[3]/span[2]/div[2]').click()
# 等待扫码登录
time.sleep(15)
def start():
search = driver.find_element_by_xpath('//*[@id="Popover1-toggle"]')
search.send_keys("金融")
search.send_keys(Keys.ENTER)
driver.implicitly_wait(6)
driver.find_element_by_xpath('//*[@id="root"]/div/main/div/div[1]/div/div/ul/li[2]/a').click()
scroll()
def scroll():
for i in range(400,2000,500):
time.sleep(1)#延时,模拟人为加载
js=f"document.documentElement.scrollTop={i}"#下拉加载
driver.execute_script(js)
user_link()
def user_link():
source = driver.page_source
html = etree.HTML(source)
for links in html.xpath('//*[@id="SearchMain"]/div/div/div/div/div'):
link = links.xpath('./div/div/div/div[2]/h2/div/span/div/div/a/@href')
if int(len(link)) !=0:
link = link[0]
print (link)
if __name__ == '__main__':
start()
第三方登录知乎
最新推荐文章于 2023-03-19 12:23:31 发布