赞
踩
前言
提到python就离不开爬虫,把想要的资料文章新闻图片视频等等都获取到并保存,百度上随便找个网站就能练练但是爬取的东西都没啥意义,好玩的网站反爬机制小白破解不了流程也复杂,接口入参加密长时间调接口ip被限制等等,但是selenium是最无敌的,走到哪几乎就能获取到哪,闲的没事写写玩
今日头条首页的头条热榜【换一批】
初始会有10条新闻,然后点击【换一批】到下一页,因为不知道具体有多少页【换一批】多少次就会重复回来,所以在代码里【换一批】写了个死循环,内容重复就停了。然后把热榜的标题和对应热榜详细的网址都存到excel中
代码
先是把启动selenium单拿出来写
from selenium import webdriver import time class Ui: def __init__(self): self.url = 'https://www.toutiao.com/' def Supply_chain(self): options = webdriver.ChromeOptions() options.add_experimental_option('excludeSwitches',['enable-logging']) #关闭打印日志 # options.add_argument('--headless') #关闭浏览器头(无头模式) test = webdriver.Chrome('drive\chromedriver.exe',options=options) test.get(self.url) test.maximize_window() time.sleep(4) return test
然后在另一个模块中引用这个Supply_chain方法,进行业务操作获取到保存就行了,每次都会创建文件夹名字用的uuid防止重复
from common.open_headlines import Ui
import xlsxwriter
import uuid
import time
import os
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。