![d286afacd864fbf47a57ebc511d174aa.png](https://i-blog.csdnimg.cn/blog_migrate/49e6725348319d40552200326176b6fe.jpeg)
俺又回来喽·(。・∀・)ノ
在昨天的二号版本中内裤没洗有点骚:爬虫在手,工作休走。python+selenium爬取前程无忧(2)有一个非常尴尬的问题,那就是爬取得到的数据的后四个容易出现重复
![506fa116937ff647634b671fc2663d13.png](https://i-blog.csdnimg.cn/blog_migrate/fafb47439040c5282b13dd75e1725e76.jpeg)
看上面图会发现后四个框里有好多“克隆”的数据,这是为什么呢?(⊙_⊙)?原因我也不太清楚,可能在for循环里使用click()容易造成卡克,导致数据读取重复吧.....可怜了我辛辛苦苦写的代码>﹏<,为了数据的准确,只能再想别的方法了。
不过这个错误的代码我还是会保存的,以后说不定能用来当障眼法。( $ _ $ )
整理思路
1.之所以数据会重复,应该是因为网址获取重复了,所以会导致后三个在同一页上的数据重复,所以得先得到正确的网址
2.试试第一条是不是对的( •̀ ω •́ )y
![68ba2a30093dc183c087ca8429b53b82.png](https://i-blog.csdnimg.cn/blog_migrate/c7ea6ec6b441e17afe48c254cd8c0a36.jpeg)
找网址其实不难,每个在class=t1里的a标签都有,但是如何把这个从新标签页里打开呢
在我不懈努力(厚颜无耻)的情况下,终于在csdn上找到了一个用js语言来打开标签的方法。
然后就简单啦,利用lxml里的etree来解析网页再用xpath提取,过程蛋疼了点就不上图了。
现在奉上代码,加了注释,免得以后自己都忘了自己写的啥o(* ̄▽ ̄*)o
一些配置代码,起名为__init__放在同级目录下
localhost="localhost"
root="root"
password="123"
database="mytest"
use="use mytest"
url="https://www.51job.com/"
Table=input("please input the table name")
INPUT= str(input("please input the profession"))
p=input("please input the place you want")
page=int(input("please input the page you want"))
drop="drop table if exists %s"%Table
create="create table %s(position varchar(255) NOT NULL,company varchar(255),workplace varchar(255),salary varchar(255),dateline varchar(255),experience varchar(255),degree varchar(255),want varchar(255),url varchar(255))"%Table
savein="insert into "+str(Table)+"(position,company,workplace,salary,dateline,experience,degree,want,url) values(%s,%s,%s,%s,%s,%s,%s,%s,%s)"
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
import pymysql
import time
from lxml import html
import random
from __init__ import *
etree=html.etree
class Qcwy():
def __init__(self):
self.conn = pymysql.connect(localhost, root, password, database) #初始化,连接数据库
self.cursor = self.conn.cursor()
self.cursor.execute(use) #选定数据库
self.cursor.execute(drop)
sql = create
self.cursor.execute(sql) #创建表
def start(self):
self.browser=webdriver.Chrome() #启动Chrome浏览器
self.browser.get(url) #获取网址:前程无忧
self.wait=WebDriverWait(self.browser,10)
input=self.wait.until(EC.element_to_be_clickable((By.ID,"kwdselectid")))
input.send_keys(INPUT) #输入查找的职位名
add=self.wait.until(EC.element_to_be_clickable((By.ID,"work_position_input")))
add.click()
time.sleep(1)
if self.browser.find_element_by_xpath('//*[@id="work_position_click_multiple_selected_each_120000"]/em'):
cancel=self.browser.find_element_by_xpath('//*[@id="work_position_click_multiple_selected_each_120000"]/em').click()
time.sleep(1) #取消定位地点
#单个选择方法:
place=self.browser.find_element_by_xpath('//*[@id="work_position_click_center_right_list_000000"]//*[@class="js_more"]/em[contains(text(),"%s")]'%p)
place.click() #选择地点
#多个选择方法暂无{{{(>_<)}}}
#随机选取5个方法:
# adds=browser.find_elements_by_xpath('//*[@data-navigation="000000"]')
# slice=random.sample(adds,5)
# for s in slice:
# s.click()
ok=self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME,"p_but")))
ok.click()
button=self.wait.until(EC.element_to_be_clickable((By.XPATH,"/html/body/div[3]/div/div[1]/div/button")))
button.click() #职位查找完毕,准备开始爬取
def crawl(self):
time.sleep(1)
i=0
lists = self.browser.find_elements_by_xpath('//*[@class="el"]') #得到所有列表
for list in lists :
if i<page:
current=self.browser.current_window_handle
positions=self.browser.find_elements_by_class_name('t1')
companies=self.browser.find_elements_by_class_name('t2') #分类
workplaces=self.browser.find_elements_by_class_name('t3')
salaries=self.browser.find_elements_by_class_name('t4')
datelines=self.browser.find_elements_by_class_name('t5')
urls=self.browser.find_elements_by_xpath('//*[@id="resultList"]/div/p/span/a')
for position,company,workplace,salary,dateline,url in zip(positions,companies,workplaces,salaries,datelines,urls):
p=position.text #得到每个分类的字符串
c=company.text
w=workplace.text
s=salary.text
d=dateline.text
u=url.get_attribute('href') #得到url
js='window.open("%s")'%u
self.browser.execute_script(js) #打开新标签页,前往职位具体信息
windows = self.browser.window_handles
self.browser.switch_to.window(windows[-1]) #切换标签页
htmlDiv=etree.HTML(self.browser.page_source)
experience=htmlDiv.xpath("/html/body/div[3]/div[2]/div[2]/div/div[1]/p[2]/text()[2]")
degree=htmlDiv.xpath("/html/body/div[3]/div[2]/div[2]/div/div[1]/p[2]/text()[3]")
want=htmlDiv.xpath("/html/body/div[3]/div[2]/div[2]/div/div[1]/p[2]/text()[4]") #得到具体信息
for e,de,wa in zip(experience,degree,want):
print(p + "t", c + "t", w + "t", s + "t", d + "t", e + "t", de + "t", wa + "t", u + "t") #输出所有信息
try:
self.cursor.execute(savein,(p,c,w,s,d,e,de,wa,u))
self.conn.commit() #存入数据库
except:
print("error")
self.browser.switch_to.window(current)
allchung=self.browser.window_handles #关闭除主页面的标签页
for handle in allchung:
if handle!=current:
self.browser.switch_to.window(handle)
self.browser.close()
self.browser.switch_to.window(current)
i=i+1
nextpage=self.wait.until(EC.element_to_be_clickable((By.LINK_TEXT,"下一页"))) #前往下一页
nextpage.click()
else:
self.browser.quit() #关闭浏览器
self.conn.close() #收工
if __name__=="__main__": #主方法
q=Qcwy()
q.start()
q.crawl()
最后做一个实站吧,还是爬在北京的Java工作,这回数据不会再错喽O(∩_∩)O
![6d134b009500b1c8c45911d425f824fe.png](https://i-blog.csdnimg.cn/blog_migrate/beb682bce05d3c1f296cb7b0960a0869.jpeg)
![ce14f210a735e046b04614685abb924d.png](https://i-blog.csdnimg.cn/blog_migrate/14b87c9a0bd21bbb4505888961a78a55.jpeg)
完成(o゚v゚)ノ
这样的话这个爬虫差不多就完工了,如果有条件的话,我会抽空加选择多个地点爬取的方法哦。