爬虫selenium基本操作

from selenium import webdriver
import time
from lxml import etree

driver = webdriver.Chrome()     #需要把Chromedriver放到python根目录。
driver.get('https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput=')
time.sleep(3)   # 等待页面打开
html = driver.page_source   # 获取页面HTML
driver.quit()   # 关闭浏览器,节省资源

hTml = etree.HTML(html)
# 职位信息  此处用xpath语法
tit_infor = hTml.xpath('//h3/text()')
# 薪资待遇
money_infor = hTml.xpath('//span[@class="money"]/text()')
# 地址
addr_infor = hTml.xpath('//span[@class="add"]/em/text()')
for title,money,addr in zip(tit_infor,money_infor,addr_infor):  # 拆包
    # print(title,money,addr)
    with open('lagou.txt','a',encoding='utf-8') as fp:  # 此处用'a',原因是可以一直往里面添加数据。
        fp.write(title+money+addr+'\n')

你可能感兴趣的:(python,python)