需求:对某网站实现动态爬取并能每天保存到文本文件中

解决方法:通过Python的BeautifulSoup、selenium完成该需求。

完整代码:

import json
import urllib.request
import urllib.error
from urllib.parse import quote
from bs4 import BeautifulSoup
from builtins import strfrom selenium import webdriver
from selenium.webdriver.common.by import By
from bs4 import BeautifulSoup
from urllib.request import urlopen
from selenium.common.exceptions import NoSuchElementException
import re
import time
import datetime
import sys
sys.setrecursionlimit(1000000)
import osfrom selenium.webdriver.common.keys import Keysdef getQuestionsLinks(driver):bs = BeautifulSoup(driver.page_source, 'lxml')AllInfo=bs.findAll('tr', {'class': 'bgcol'})linkyesterdaystr = str(datetime.date.today() - datetime.timedelta(days=0)) ###0代表当天,1代表昨天print(linkyesterdaystr)firstdaystr=AllInfo[1].get_text().replace('\t','').replace('\n','|').split('|')[-2]print(firstdaystr)com = int(linkyesterdaystr.__eq__(firstdaystr))if (com == 0):returnfor info in AllInfo: #[0:2]if info.find('a', {'class':'xjxd_nr'}) is None:print("No usefull Info")else:paras=info.find('a', {'class': 'xjxd_nr'}).get('onclick').replace('detail(','').replace("'",'')[0:-2]listparas=paras.split(',')innerlink='http://www.shenl.com.cn/todetail?id='+listparas[0]+'&isSearchPassWord='+listparas[1]+'&tag='+listparas[2]innerDetail=info.get_text().replace('\t','').replace('\n','|').split('|')while '' in innerDetail:innerDetail.remove('')innerdate = time.strptime(innerDetail[-1], "%Y-%m-%d") #Struct_timeprint(innerdate)firstdaydate = time.strptime(firstdaystr, "%Y-%m-%d")print(firstdaydate)depart=(datetime.datetime(*firstdaydate[:3]) - datetime.datetime(*innerdate[:3])).daysif depart > 0: #如先获得首页里的开始时间,如果首次出现时间小于开始时间则停止爬虫returnf.write('\t'.join(innerDetail) + "\t" + innerlink + "\n")try:print(type(driver.find_element(By.LINK_TEXT, "下一页")))driver.find_element_by_xpath("//a[contains(text(),'下一页')]").click()except NoSuchElementException:time.sleep(1)print("No more pages found")returntime.sleep(4)getQuestionsLinks(driver)if __name__ == '__main__':for n in range(0,1,1):import timeIsoTimeFormat = '%Y_%m_%d'f = open('G:\\temp\\Question_Incr_'+str(time.strftime(IsoTimeFormat))+'.txt', 'w', encoding='utf-8')driver = webdriver.Chrome("C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe")driver.get("http://www.shenl.com.cn/xjxdList")time.sleep(3)getQuestionsLinks(driver)driver.close()f.close()

基于某网站的信息爬取与保存相关推荐

  1. 基于某网站的信息爬取与保存_指定查询内容

    需求:对某网站实现输入指定的查询内容后动态爬取并能保存到文本文件中 解决方法:通过Python的BeautifulSoup.selenium的Kyes完成该需求. 代码见下: import json ...

  2. 基于某网站的信息爬取与保存_指定跳转页

    需求:对某网站实现输入指定的跳转页完成爬取并能保存到文本文件中 解决方法:通过Python的BeautifulSoup.selenium的Kyes完成该需求. 代码见下: import json im ...

  3. 基于python的汽车信息爬取与可视化分析系统

    温馨提示:文末有 CSDN 平台官方提供的学长 Wechat / QQ 名片 :) 1. 项目简介 本项目利用网络爬虫技术从某汽车门户网站采集汽车数据,并利用 Flask + Echarts 前后端框 ...

  4. 基于python的汽车销售_基于Python的汽车信息爬取与分析

    二.<基于Python的汽车数据爬取与分析> 1 课题内容和要求 1.1问题的提出 1)用Python 语言自行编写爬虫框架或使用Scrapy 框架,爬取汽车之家或易车网的车辆相关数据,按 ...

  5. Python 爬虫小练习:基于 XPath 的表格信息爬取

    文章目录 确定目标和分析思路 目标 思路 观察情况 爬取名单表 爬取详情页二级信息 爬虫请遵守相关法律法规,不要做违法犯罪的事情 爬虫小技巧总结 这是一个 Python 爬虫的入门练习,我们通过 Re ...

  6. 爬虫-菜谱信息爬取(保存至数据库)

    目录 爬虫爬取思路 python代码 数据库代码 后期发现: 解决方法: 词云制作 爬虫爬取思路 python代码 import requests # 请求 from lxml import etre ...

  7. 基于scrapy下的租房信息爬取与数据展示工具的设计与实现

    环境:python 3.6.0 Anaconda custom 64bit 4.3.0 Pycharm x64 专业版 2018.1.2 Web strom x64 专业版 2018.1.3 scra ...

  8. Crawler:爬虫之基于https+parse库实现爬取国内某知名招聘网上海、北京关于区块链职位的求职信息

    Crawler:爬虫之基于https+parse库实现爬取国内某知名招聘网上海.北京关于区块链职位的求职信息 目录 输出结果 1.上海区块链职位 2.北京区块链职位 设计思路 核心代码 输出结果 1. ...

  9. python爬取微博用户正文_基于Python的新浪微博用户信息爬取与分析

    基于 Python 的新浪微博用户信息爬取与分析 邓文萍 [摘 要] 摘要:本文设计并实现了一个微博用户信息爬取与分析系统 , 利用 Cookie 实现了用户的模拟登录 , 使用 Python 语言的 ...

最新文章

  1. Educational Codeforces Round 54
  2. 高能预警!各路大神正火速奔赴8月WOT2016 移动互联网技术峰会
  3. win8计算机管理没有用户组,Win8右键计算机管理提示“该文件没有与之关联的程序”怎么办?...
  4. localhost与127.0.0.1的区别
  5. 作者:周虎,就职于山东农业大学农业大数据研究中心。
  6. Yann LeCun 会成为下一个居里夫人吗?
  7. 最前沿的容器技术有哪些?腾讯、华为、思科等 6 位顶尖专家为你解答!
  8. Laravel nginx 伪静态规则
  9. linux下SVN CVS命令大全
  10. opera价格设置(一)
  11. 今天,正式开始行走边境线!
  12. 中国手机沉浮录:告别青铜时代
  13. 联想笔记本e43l_联想昭阳E43L电脑配置
  14. 移动公网5G配置(一)
  15. 2021-07-30 es6 -第4章和String复习
  16. You can‘t specify target table ‘Person‘ for update in FROM clause
  17. 实验1 进程管理实验-计算机操作系统
  18. 解决java编译错误( 程序包javax.servlet不存在javax.servlet.*)
  19. Python中使用Scrapy爬虫抓取上海链家房价信息
  20. 2023年全国最新会计专业技术资格精选真题及答案1

热门文章

  1. wxWidgets:wxSizer类用法
  2. boost::signals2模块thread_safe_signals 库替代线程模型的基本测试
  3. boost::math::inverse_chi_squared用法的测试程序
  4. boost::log::expressions::channel_severity_filter_actor用法的测试程序
  5. boost::hana::second用法的测试程序
  6. boost::two_graphs_common_spanning_trees用法的测试程序
  7. boost::gil模块临界点threshold的测试程序
  8. GDCM:gdcm::IconImageGenerator的测试程序
  9. boost::contract模块实现private protected的测试程序
  10. Boost:构造一个流对象,任何发送到此流将标准输出