设计制作简单的手机网站,公司网站开发费用兴田德润在哪儿,wordpress插件不会用,做外汇网站这段代码是使用Selenium自动化测试模块进行网页爬取的示例代码。它通过模拟人的行为在浏览器中操作网页来实现爬取。具体的流程如下#xff1a;
导入所需的模块#xff0c;包括Selenium、时间、随机、csv等模块。打开浏览器#xff0c;创建一个Chrome浏览器实例。设置要爬取…这段代码是使用Selenium自动化测试模块进行网页爬取的示例代码。它通过模拟人的行为在浏览器中操作网页来实现爬取。具体的流程如下
导入所需的模块包括Selenium、时间、随机、csv等模块。打开浏览器创建一个Chrome浏览器实例。设置要爬取的页数范围。循环遍历每一页的URL。访问每一页的URL获取网页数据。创建一个CSV文件设置字段名。获取每个职位的详情页URL。遍历每个详情页URL发送请求获取响应数据。使用css选择器解析响应数据提取所需的数据内容。将提取到的数据写入CSV文件。打印出职位的相关信息。 该代码的主要功能是爬取招聘网上的职位信息包括职位名、薪资、城市、经验、学历、福利、岗位标签、公司名、详情页等信息。使用了Selenium模拟人的行为通过使用开发者工具获取到的CSS选择器来定位和提取数据。 # 导入自动化测试模块
from selenium import webdriver
# 导入时间模块
import time
# 导入随机模块
import random
# 导入csv模块 内置模块 不需要安装
import csv
import requests
import parselselenium: 模拟人的行为去操作浏览器# 1. 打开浏览器
driver webdriver.Chrome()# 设置页数范围
start_page 0
end_page 10 # 假设要爬取前5页的数据
for page in range(start_page, end_page):# 2. 访问网站url fhttps://www.liepin.com/zhaopin/?city070020dq070020pubTimecurrentPage{page}pageSize40key%E8%B4%A2%E5%8A%A1bpsuggestTagworkYearCode0compIdcompNamecompTagindustrysalaryjobKindcompScalecompKindcompStageeduLevelotherCitysfromsearch_job_pcckIdvda04kszzsgxhhl7nc4fc21r5hthguv9sceneconditionskIdvda04kszzsgxhhl7nc4fc21r5hthguv9fkIdvda04kszzsgxhhl7nc4fc21r5hthguv9suggestIddriver.get(url)# 隐式等待 --- 让网页数据加载完成driver.implicitly_wait(10)time.sleep(3)# 创建文件f open(data.csv, modea, encodingutf-8, newline)csv_writer csv.DictWriter(f, fieldnames[职位名,薪资,城市,经验,学历,福利,岗位标签,公司名,详情页,])# 写入表头csv_writer.writeheader()# 3. 获取岗位详情页url地址url_list driver.find_elements(css selector, .job-detail-box a)for index in url_list:url index.get_attribute(href)print(url)time.sleep(random.randint(1, 2))1. 发送请求, 模拟浏览器对 url地址 发送请求- 把python代码伪装成浏览器发送请求目的: 为了防止被反爬# 请求url地址# url https://www.liepin.com/job/1948917627.shtml?d_sfromsearch_primed_ckIdnulld_curPage2d_pageSize40d_headIdnulld_posi1skIds5h3mfxh8n1c3ec3dr7nnc6d4lycb9dbfkIds5h3mfxh8n1c3ec3dr7nnc6d4lycb9dbckIds5h3mfxh8n1c3ec3dr7nnc6d4lycb9dbsfromsearch_job_pccurPage2pageSize40index1# 模拟伪装 --- 开发者工具里面进行复制粘贴headers {User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36,}# 发送请求 - Response [200] 表示请求成功response requests.get(urlurl, headersheaders)2. 获取数据, 获取服务器返回响应数据开发者工具: responseresponse.text 获取响应文本数据, 返回字符串数据类型 html字符串数据内容3. 解析数据, 提取我们想要的数据内容css选择器 根据标签属性提取数据内容:# 把获取下来 html字符串数据内容 response.text 转成可解析对象selector parsel.Selector(response.text).job-apply-content .name-box .name 定位标签- get() 获取第一个标签 就获取一个内容 返回字符串- getall 获取所有标签内容, 返回列表css选择器, 在系统课程 都是从头到尾讲2.5个小时才能讲完知识点内容a::text 表示 提取a标签里面文本呀title selector.css(.job-apply-content .name-box .name::text).get() # 职位名salary selector.css(.job-apply-content .name-box .salary::text).get() # 薪资city selector.css(.job-apply-content .job-properties span:nth-child(1)::text).get() # 城市exp selector.css(.job-apply-content .job-properties span:nth-child(3)::text).get() # 经验edu selector.css(.job-apply-content .job-properties span:nth-child(5)::text).get() # 学历# 把列表合并成字符串labels ,.join(selector.css(.job-apply-container-desc .labels span::text).getall()) # 福利job_labels ,.join(selector.css(.tag-box ul li::text).getall()) # 职位标签company selector.css(.company-info-container .company-card .content .name::text).get() # 公司名job_info \n.join(selector.css(.job-intro-container .paragraph dd::text).getall()) # 岗位职业4. 保存数据, 把数据保存本地文件- 基本数据 保存csv表格里面- 岗位职责 保存文本里面# 把数据写入到字典里面dit {职位名: title,薪资: salary,城市: city,经验: exp,学历: edu,福利: labels,岗位标签: job_labels,公司名: company,详情页: url,}# 写入数据csv_writer.writerow(dit)print(title, salary, city, exp, edu, labels, job_labels, company, job_info)