问题
1.爬取时进行循环翻页时找不到结束条件
解决方案:当点击下一页有效时 按钮的类为’pager_next ’ 而当点击下一页按钮无效时,该按钮的类变成了’pager_next pager_next_disabled’
所以在获取按钮后获取该按钮的属性,如果该按钮的类为’pager_next’则执行click,若类不为’pager_next’ 则break循环。
btn=web.find_element_by_class_name('pager_next ')
if(btn.get_attribute('class')!='pager_next '):
web.close()#关闭浏览器
break
else:
btn.click()
2.爬取时突然需要登录
其实不需要登录账号也可以爬取信息,但是在爬取过程中会突然弹出登录网页。所以就需要在进入网页时登录,也就是设置cookie,而cookie需要先登录后才可以获取,所以就写了个获取cookie的代问,先获取cookie后再在爬虫时设置cookie就不会弹登录了
获取cookie
from selenium.webdriver import Chrome
import time
import json
web=Chrome()
web.get('https://www.lagou.com/')
with open('cookies.txt','w') as f:
f.write(json.dumps(web.get_cookies()))
web.close()
设置cookie
web.delete_all_cookies()
with open('cookies.txt','r' ) as f:
cookies_list=json.load(f)
for cookie in cookies_list:
if 'expiry' in cookie:
del cookie['expiry']
web.add_cookie(cookie)
3.‘gbk’ codec can’t encode character ‘\u2022’ in position 545262: illegal multibyte sequence
解决方案:打开文件时设置编码格式
with open('contents/内容_%s.txt' % count, 'w',encoding='utf-8') as f:
f.write(text)
4.Element is not clickable at point,Other element would receive the click: xxx
自己感觉就是还没加载出来
然后休息了1s再点击好像就没问题了
解决方案:
time.sleep(2)
a.find_element_by_tag_name('h3').click()
全部代码
获取cookie
# -*- coding=utf-8 -*-
# @Time :2021/3/2911:36
# @Author :Asuna
# @File:getCookie.py
# @Software: PyCharm
from selenium.webdriver import Chrome
import time
import json
web=Chrome()
web.get('https://www.lagou.com/')
#关闭一开始那个窗口
web.find_element_by_xpath('//*[@id="cboxClose"]').click()
time.sleep(1)
#点击登录
web.find_element_by_xpath('//*[@id="lg_tbar"]/div[1]/div[2]/ul/li[2]/a').click()
time.sleep(1)
input=web.find_elements_by_class_name('login_enter_password')
input[0].send_keys('输入你的账号')
input[1].send_keys('输入你的密码')
web.find_element_by_xpath('/html/body/div[3]/div[1]/div/div/div[2]/div[3]/div[2]/div[2]/div[2]').click()
time.sleep(20)
with open('cookies.txt','w') as f:
f.write(json.dumps(web.get_cookies()))
web.close()
爬取页面
# -*- coding=utf-8 -*-
# @Time :2021/3/2519:51
# @Author :Asuna
# @File:main.py
# @Software: PyCharm
from selenium.webdriver import Chrome
from selenium.webdriver.common.keys import Keys
import time
import json
count=1
#创建浏览器对象,打开并访问指定网页
web=Chrome()
web.get('https://www.lagou.com/')
#关闭一开始那个窗口
web.find_element_by_xpath('//*[@id="cboxClose"]').click()
web.delete_all_cookies()
with open('cookies.txt','r' ) as f:
cookies_list=json.load(f)
for cookie in cookies_list:
if 'expiry' in cookie:
del cookie['expiry']
web.add_cookie(cookie)
#休息一秒,避免了关了一开始的窗口后不能马上找到输入框
time.sleep(1)
#找到搜索窗口
web.find_element_by_xpath('//*[@id="search_input"]').send_keys('sql',Keys.ENTER)
while(True):
time.sleep(5)
alist = web.find_elements_by_class_name('position_link')
# 在每个class=‘position_link’的a标签下面找h3标签
for a in alist:
time.sleep(2)
a.find_element_by_tag_name('h3').click()
# 跳转到新打开的页面
web.switch_to.window(web.window_handles[-1])
# 获取所需内容
text = web.find_element_by_xpath('//*[@id="job_detail"]/dd[2]').text
# 保存内容
with open('contents/内容_%s.txt' % count, 'w',encoding='utf-8') as f:
f.write(text)
# 关闭该网页
web.close()
# 回到之前的页面
web.switch_to.window(web.window_handles[0])
# 编号加1
count += 1
btn=web.find_element_by_class_name('pager_next ')
if(btn.get_attribute('class')!='pager_next '):
web.close()#关闭浏览器
break
else:
btn.click()