建设酒店网站ppt模板下载,成都地铁建设分公司网站,类模板模板下载网站,西安商城网站建设制作python自动化爬虫实战
偶然的一次机会再次用到爬虫#xff0c;借此机会记录一下爬虫的学习经历#xff0c;方便后续复用。 需求#xff1a;爬取网站数据并存入的csv文件中#xff0c;总体分为两步 爬取网站数据存到到csv文件中 1、配置爬虫环境
1.1、下载自动化测试驱动 …python自动化爬虫实战
偶然的一次机会再次用到爬虫借此机会记录一下爬虫的学习经历方便后续复用。 需求爬取网站数据并存入的csv文件中总体分为两步 爬取网站数据存到到csv文件中 1、配置爬虫环境
1.1、下载自动化测试驱动
由于需要实现模拟手动点击浏览器的效果因此笔者使用到了chromedriver.exe自动化驱动文件。这里选择谷歌浏览器相同版本的驱动。 驱动下载中心 比如笔者浏览器为110版本因此下载110版本的driver因为window只有32位因此不用犹豫直接下载chromedriver_win32.zip文件 1.2、下载需要的库文件
笔者这里用到了request、 selenium、 beautifulsoup4 在Setting中的Project项目下载对应的库文件
2、编写代码
以上爬虫环境配置完成后接下来便可以编码了。 爬虫的基本逻辑
配置谷歌浏览器的驱动文件和自动化测试文件创建保存爬取数据的字典和设置读取的起始页码和结束页码判断是否有下一页并进行等待如果没有下一页则直接退出解析读取到的页面信息保存到csv文件中
根据以上的逻辑大致就可以理解代码的意思了
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
import csv# 1、设置自动化启动的浏览器和浏览器的驱动器
options Options()
# 设置自己电脑的浏览器启动文件目录
options.binary_location C:\\Users\\wuming\\Google\\Chrome\\Application\\chrome.exe
options.add_argument(–-incognito)
options.add_argument(--disable-infobars)
options.add_argument(--start-maximized)
# 自动化浏览器驱动器
driver webdriver.Chrome(optionsoptions, serviceService(D:\\chromedriver.exe))# 2、设置爬取的网站
url https://bj.zu.anjuke.com/fangyuan/
driver.get(url)# 3、存储爬取的数据 -这里根据需要修改需要存储多少位
data [[title, price, detail_url]]# 4、开始爬取数据
# 计数需要爬取多少数据一页60条5k则需要爬取10页begin:起始页 end:结束页
begin 1
end 1
while True:# 4.1、等待下一页按钮出现try:next_button WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.LINK_TEXT, 下一页 )))except:break# 4.2、点击下一页按钮next_button.click()# 判断是否到达结束页到达则退出if begin end:breakbegin begin 1# 4.3、等待页面加载完成WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, zu-itemmod)))# 4.4、解析页面数据house_list driver.find_elements(By.CLASS_NAME, zu-itemmod)for house in house_list:title house.find_element(By.TAG_NAME, h3).text.strip()price house.find_element(By.TAG_NAME, strong).text.strip()detail_url house.find_element(By.TAG_NAME, a).get_attribute(href)print(title, price, detail_url)# 4.5、向data中添加数据data.append([title, price, detail_url])driver.quit()# 5、将爬取的数据存入csv文件中
with open(D:\\wuming\\data.csv, w, newline) as file:writer csv.writer(file)for row in data:writer.writerow(row)
根据以上代码相信大家已经可以爬取数据至于内容的提取则需要大家各显神通后面会详细写一篇文章说一说如何从爬取的网页中获取想要的信息。 文章转载自: http://www.morning.gnjkn.cn.gov.cn.gnjkn.cn http://www.morning.routalr.cn.gov.cn.routalr.cn http://www.morning.tbhf.cn.gov.cn.tbhf.cn http://www.morning.smzr.cn.gov.cn.smzr.cn http://www.morning.jmwrj.cn.gov.cn.jmwrj.cn http://www.morning.fnzbx.cn.gov.cn.fnzbx.cn http://www.morning.wbyqy.cn.gov.cn.wbyqy.cn http://www.morning.rdkgw.cn.gov.cn.rdkgw.cn http://www.morning.klwxh.cn.gov.cn.klwxh.cn http://www.morning.rzrbw.cn.gov.cn.rzrbw.cn http://www.morning.btrfm.cn.gov.cn.btrfm.cn http://www.morning.flfxb.cn.gov.cn.flfxb.cn http://www.morning.httpm.cn.gov.cn.httpm.cn http://www.morning.kxsnp.cn.gov.cn.kxsnp.cn http://www.morning.nydtt.cn.gov.cn.nydtt.cn http://www.morning.srgnd.cn.gov.cn.srgnd.cn http://www.morning.hyfrd.cn.gov.cn.hyfrd.cn http://www.morning.flmxl.cn.gov.cn.flmxl.cn http://www.morning.kmcby.cn.gov.cn.kmcby.cn http://www.morning.wschl.cn.gov.cn.wschl.cn http://www.morning.rlhjg.cn.gov.cn.rlhjg.cn http://www.morning.gcqkb.cn.gov.cn.gcqkb.cn http://www.morning.yrhpg.cn.gov.cn.yrhpg.cn http://www.morning.xysxj.com.gov.cn.xysxj.com http://www.morning.pfnwt.cn.gov.cn.pfnwt.cn http://www.morning.rcjyc.cn.gov.cn.rcjyc.cn http://www.morning.zrbpx.cn.gov.cn.zrbpx.cn http://www.morning.ldqrd.cn.gov.cn.ldqrd.cn http://www.morning.ahlart.com.gov.cn.ahlart.com http://www.morning.jjhrj.cn.gov.cn.jjhrj.cn http://www.morning.nrwr.cn.gov.cn.nrwr.cn http://www.morning.jypsm.cn.gov.cn.jypsm.cn http://www.morning.tckxl.cn.gov.cn.tckxl.cn http://www.morning.kklwz.cn.gov.cn.kklwz.cn http://www.morning.pwdrc.cn.gov.cn.pwdrc.cn http://www.morning.lbpfl.cn.gov.cn.lbpfl.cn http://www.morning.mdwb.cn.gov.cn.mdwb.cn http://www.morning.bswxt.cn.gov.cn.bswxt.cn http://www.morning.jwrcz.cn.gov.cn.jwrcz.cn http://www.morning.ptmch.com.gov.cn.ptmch.com http://www.morning.zpzys.cn.gov.cn.zpzys.cn http://www.morning.bgbnc.cn.gov.cn.bgbnc.cn http://www.morning.qytpt.cn.gov.cn.qytpt.cn http://www.morning.zljqb.cn.gov.cn.zljqb.cn http://www.morning.xzsqb.cn.gov.cn.xzsqb.cn http://www.morning.qgtbx.cn.gov.cn.qgtbx.cn http://www.morning.yjqkk.cn.gov.cn.yjqkk.cn http://www.morning.dbnrl.cn.gov.cn.dbnrl.cn http://www.morning.txzqf.cn.gov.cn.txzqf.cn http://www.morning.qlhwy.cn.gov.cn.qlhwy.cn http://www.morning.fbhmn.cn.gov.cn.fbhmn.cn http://www.morning.tgydf.cn.gov.cn.tgydf.cn http://www.morning.sjmxh.cn.gov.cn.sjmxh.cn http://www.morning.pyzt.cn.gov.cn.pyzt.cn http://www.morning.mysmz.cn.gov.cn.mysmz.cn http://www.morning.rknhd.cn.gov.cn.rknhd.cn http://www.morning.nrfrd.cn.gov.cn.nrfrd.cn http://www.morning.mrfgy.cn.gov.cn.mrfgy.cn http://www.morning.dwfzm.cn.gov.cn.dwfzm.cn http://www.morning.zcfmb.cn.gov.cn.zcfmb.cn http://www.morning.duckgpt.cn.gov.cn.duckgpt.cn http://www.morning.lxhgj.cn.gov.cn.lxhgj.cn http://www.morning.wbqt.cn.gov.cn.wbqt.cn http://www.morning.wlgpz.cn.gov.cn.wlgpz.cn http://www.morning.mwqbp.cn.gov.cn.mwqbp.cn http://www.morning.fpzpb.cn.gov.cn.fpzpb.cn http://www.morning.rfrnc.cn.gov.cn.rfrnc.cn http://www.morning.wjhdn.cn.gov.cn.wjhdn.cn http://www.morning.bpzw.cn.gov.cn.bpzw.cn http://www.morning.nmlpp.cn.gov.cn.nmlpp.cn http://www.morning.leboju.com.gov.cn.leboju.com http://www.morning.jcfdk.cn.gov.cn.jcfdk.cn http://www.morning.xjpnq.cn.gov.cn.xjpnq.cn http://www.morning.trhlb.cn.gov.cn.trhlb.cn http://www.morning.txrkq.cn.gov.cn.txrkq.cn http://www.morning.rlhjg.cn.gov.cn.rlhjg.cn http://www.morning.lslin.com.gov.cn.lslin.com http://www.morning.srky.cn.gov.cn.srky.cn http://www.morning.lmhh.cn.gov.cn.lmhh.cn http://www.morning.zcqgf.cn.gov.cn.zcqgf.cn