python实现列表页数据的批量抓取,练手的,下回带分页的

#!/usr/bin/env python
# coding=utf-8 import requests
from bs4 import BeautifulSoup
import pymysql import sys, io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf8') # Change default encoding to utf8 print('连接到mysql服务器...')
db = pymysql.connect("localhost","root","root","python")
print('连接上了!')
cursor = db.cursor() hdrs = {'User-Agent':'Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)'} url = "http://www.zztez.com/tezgl/" r = requests.get(url, headers = hdrs)
soup = BeautifulSoup(r.content.decode('gbk', 'ignore'), 'lxml') def has_class_but_no_id(tag):
return tag.has_attr('title') and tag.has_attr('href') and not tag.has_attr('target') urls = []
for link in soup.find_all(has_class_but_no_id):
url="http://www.zztez.com" + link.get('href')
r = requests.get(url, headers = hdrs)
soup = BeautifulSoup(r.content.decode('gbk', 'ignore'), 'lxml') title=soup.find("h1")
title=title.string.encode("utf-8") intro=soup.select(".intro")
rintro=intro[0].string.encode("utf-8") content=soup.select(".content")
rcontent=content[0].encode("utf-8") #查询数据
sql="SELECT count(*) as total FROM article WHERE title like %s"
data=(title)
row_affected=cursor.execute(sql,data)
one=cursor.fetchone() if one==(0,):
insert = ("INSERT INTO article(title,intro,content)" "VALUES(%s,%s,%s)")
data = (title, rintro, rcontent)
cursor.execute(insert, data)
db.commit() print('爬取数据并插入mysql数据库完成...')

python实现列表页数据的批量抓取练手练手的的更多相关文章

  1. Web Scraper 翻页——控制链接批量抓取数据

    ![](https://image-1255652541.cos.ap-shanghai.myqcloud.com/images/20190708214014.png) 这是简易数据分析系列的第 5 ...

  2. Web Scraper 翻页——控制链接批量抓取数据(Web Scraper 高级用法)| 简易数据分析 05

    这是简易数据分析系列的第 5 篇文章. 上篇文章我们爬取了豆瓣电影 TOP250 前 25 个电影的数据,今天我们就要在原来的 Web Scraper 配置上做一些小改动,让爬虫把 250 条电影数据 ...

  3. 使用HtmlAgilityPack批量抓取网页数据

    原文:使用HtmlAgilityPack批量抓取网页数据 相关软件点击下载登录的处理.因为有些网页数据需要登陆后才能提取.这里要使用ieHTTPHeaders来提取登录时的提交信息.抓取网页  Htm ...

  4. thinkphp5 列表页数据分页查询3-带搜索条件

    先加载模板然后在前端HTML页面请求数据 /** * 加载列表页模板 * @author 冯广福 */ public function index() { LogWriteService::write ...

  5. Hibernate学习---第十一节:Hibernate之数据抓取策略&批量抓取

    1.hibernate 也可以通过标准的 SQL 进行查询 (1).将SQL查询写在 java 代码中 /** * 查询所有 */ @Test public void testQuery(){ // ...

  6. Python3利用BeautifulSoup4批量抓取站点图片的代码

    边学边写代码,记录下来.这段代码用于批量抓取主站下所有子网页中符合特定尺寸要求的的图片文件,支持中断. 原理很简单:使用BeautifulSoup4分析网页,获取网页<a/>和<im ...

  7. [Python爬虫] 之九:Selenium +phantomjs抓取活动行中会议活动(单线程抓取)

    思路是这样的,给一系列关键字:互联网电视:智能电视:数字:影音:家庭娱乐:节目:视听:版权:数据等.在活动行网站搜索页(http://www.huodongxing.com/search?city=% ...

  8. php中CURL技术模拟登陆抓取数据实战,抓取某校教务处学生成绩。

    这两天有基友要php中curl抓取教务处成绩的源码,用于微信公众平台的开发.下面笔者只好忍痛割爱了.php中CURL技术模拟登陆抓取数据实战,抓取沈阳工学院教务处学生成绩. 首先,教务处登录需要验证码 ...

  9. 如何上传Packages到PyPI并批量抓取

    1.如何上传包到PyPI ? 更新中... 2.批量抓取simple网站第三方模块 https://pypi.python.org/simple/ 3. 第三方模块的安装和使用 python  set ...

随机推荐

  1. 本地和服务器(ubuntu)文件同步

    秘钥登录远端服务器 rsync -avze 'ssh -i ./id_rsa' root@remoteIp:/xx/remotefile.txt ./localpath (./id_rsa为本地秘钥路 ...

  2. Slack 开发入门之 Incoming Webhooks:往 Slack 的 Channel 中发消息

    一个工程师团队使用 Slack 进行团队协作比 QQ / 微信流的效率高多啦.除了基本的 IM 之外,它的扩展性也是非常重要的一点. 本文介绍 Slack 的开发入门:Incoming Webhook ...

  3. VS2013、VS2015中,新建项目没有看到解决方案的问题(已解决)

    问题描述:装好VS2013 专业版 for Update5不知怎么弄的,突然新建项目没有了解决方案,于是各种折腾,最后终于解决了! ================================== ...

  4. CH3602 Counting Swaps

    题意 3602 Counting Swaps 0x30「数学知识」例题 背景 https://ipsc.ksp.sk/2016/real/problems/c.html Just like yeste ...

  5. flash流媒体资料

    http://www.docin.com/p-111227070.html Java mina和c++ ace做socket长连接 测试报告: http://www.iteye.com/problem ...

  6. 记录一下 FastAdmin getOriginData 问题

    记录一下 FastAdmin getOriginData 问题 FastAdmin 对 用户端新增了一个 money 字段,但在后台修改时出错,提示没有 getOriginData 方法. 跟踪了一下 ...

  7. os层删除与数据库层drop的区别于对策。

    os rm删除的,可以通过os层面恢复(句柄.inode.如果inode没有了可以根据block去重组出来数据文件),也可以通过rman备份还原方式恢复.  db 层面drop删除的 只能不完全恢复( ...

  8. yii2 rbac权限管理学习笔记

    下面介绍一个 yii2 的 Rbac 权限管理设置,闲话少说,直接上代码, 1.首先我们要在组件里面配置一下 Rbac ,如下所示(common/config/main-local.php或者main ...

  9. led的驱动及测试程序

    一.驱动源码 #include <linux/module.h> #include <linux/kernel.h> #include <linux/fs.h> # ...

  10. Oracle-EXP-00011 表不存在

    Oracle-EXP-00011 表不存在 点我,点我~