from selenium import webdriver
from selenium.webdriver import ChromeOptions
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import time
from bs4 import BeautifulSoup
import re
#设置参数 excludeSwitches达到selenium被反爬(在这个地方卡了好久)
option = ChromeOptions()
option.add_experimental_option('excludeSwitches', ['enable-automation'])
option.add_argument("--disable-blink-features")
option.add_argument("--disable-blink-features=AutomationControlled")
desired_capabilities = DesiredCapabilities.CHROME
desired_capabilities["pageLoadStrategy"] = "none"
#打开谷歌浏览器
driver=webdriver.Chrome(options=option)
#打开网页
driver.get('https://ibaotu.com/sy/17-0-0-0-0-112.html')
#为div弹框,直接是与定位其他元素一样,直接进行定位('class name')
#选择QQ登录
driver.find_element_by_css_selector('body > div.re-popbox.reg-pop.login-New > div > div.login-in-way.clearfix > a.in-way-WX.ibaotu-md-click.auth-type-QQ.btn-social-login-item').click()
#浏览器窗口切换?切换到 QQ
current_window=driver.window_handles
print(current_window)
driver.implicitly_wait(5)
driver.switch_to.window(current_window[1])
#测试一下 driver.get_screenshot_as_file ("D:/1.PNG")
#再次切换到网页内部的iframe窗口
driver.switch_to.frame(0)
driver.find_element_by_css_selector('#switcher_plogin').click()
current_window1=driver.window_handles
print(current_window1)
driver.switch_to.window(current_window[1])
#driver.get_screenshot_as_file ("D:/2.PNG")
#登陆进去
username='351916740'
password='920825ZlyTC10'
time.sleep(3)
driver.switch_to.frame(0)#再一次踩坑,登录框框有iframe,需要先进入到iframe,再去定位
driver.find_element_by_css_selector('#u').send_keys(username)
time.sleep(2)
driver.find_element_by_css_selector('#p').send_keys(password)
time.sleep(1)
driver.find_element_by_id("login_button").click()
time.sleep(3)
current_window2=driver.window_handles
print(current_window2)
driver.switch_to.window(current_window[0])
all_window_height = [] # 创建一个列表,用于记录每一次拖动滚动条后页面的最大高度
all_window_height.append(driver.execute_script("return document.body.scrollHeight;")) #当前页面的最大高度加入列表
while True:
driver.execute_script("scroll(0,1000)") # 执行拖动滚动条操作,滚动条底部的参数改成了1000000,终于没加载一半就停止了!
time.sleep(3)
check_height = driver.execute_script("return document.body.scrollHeight;")
if check_height == all_window_height[-1]: #判断拖动滚动条后的最大高度与上一次的最大高度的大小,相等表明到了最底部
break
else:
all_window_height.append(check_height) #如果不想等,将当前页面最大高度加入列表。
#解析数据部分
driver.enconding='UTF-8'
soup=BeautifulSoup(driver.page_source,'html.parser')#得到全部的element代码
body=soup.find('div',attrs={'class':'skin-wrap body-background-gradient'})
body=body.find('div',attrs={'class':'search-list box-bg-search box-bottom-gradient clearfix'})#find只会找到与它内容匹配的第一个
body=body.find_all('div',attrs={'class':'hover-pop'})
for hover in body:
a_label=hover.find('a')
#print(hover.find('a'))
pattern = re.compile(r'\d+')
id=pattern.findall((a_label['href'])[0])
str_id=str(id)
dwonload_url='https:' + '//ibaotu.com/?m=downloadopen&a=open&id=' + str_id + '&down_type=1&&attachment_id=&zt_size_type=0'
print(dwonload_url)
爬虫利用selenium模拟登录
最新推荐文章于 2024-08-21 14:46:37 发布