python实现列表页数据的批量抓取练手练手的
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实现列表页数据的批量抓取练手练手的的更多相关文章
- Web Scraper 翻页——控制链接批量抓取数据
 这是简易数据分析系列的第 5 ...
- Web Scraper 翻页——控制链接批量抓取数据(Web Scraper 高级用法)| 简易数据分析 05
这是简易数据分析系列的第 5 篇文章. 上篇文章我们爬取了豆瓣电影 TOP250 前 25 个电影的数据,今天我们就要在原来的 Web Scraper 配置上做一些小改动,让爬虫把 250 条电影数据 ...
- 使用HtmlAgilityPack批量抓取网页数据
原文:使用HtmlAgilityPack批量抓取网页数据 相关软件点击下载登录的处理.因为有些网页数据需要登陆后才能提取.这里要使用ieHTTPHeaders来提取登录时的提交信息.抓取网页 Htm ...
- thinkphp5 列表页数据分页查询3-带搜索条件
先加载模板然后在前端HTML页面请求数据 /** * 加载列表页模板 * @author 冯广福 */ public function index() { LogWriteService::write ...
- Hibernate学习---第十一节:Hibernate之数据抓取策略&批量抓取
1.hibernate 也可以通过标准的 SQL 进行查询 (1).将SQL查询写在 java 代码中 /** * 查询所有 */ @Test public void testQuery(){ // ...
- Python3利用BeautifulSoup4批量抓取站点图片的代码
边学边写代码,记录下来.这段代码用于批量抓取主站下所有子网页中符合特定尺寸要求的的图片文件,支持中断. 原理很简单:使用BeautifulSoup4分析网页,获取网页<a/>和<im ...
- [Python爬虫] 之九:Selenium +phantomjs抓取活动行中会议活动(单线程抓取)
思路是这样的,给一系列关键字:互联网电视:智能电视:数字:影音:家庭娱乐:节目:视听:版权:数据等.在活动行网站搜索页(http://www.huodongxing.com/search?city=% ...
- php中CURL技术模拟登陆抓取数据实战,抓取某校教务处学生成绩。
这两天有基友要php中curl抓取教务处成绩的源码,用于微信公众平台的开发.下面笔者只好忍痛割爱了.php中CURL技术模拟登陆抓取数据实战,抓取沈阳工学院教务处学生成绩. 首先,教务处登录需要验证码 ...
- 如何上传Packages到PyPI并批量抓取
1.如何上传包到PyPI ? 更新中... 2.批量抓取simple网站第三方模块 https://pypi.python.org/simple/ 3. 第三方模块的安装和使用 python set ...
随机推荐
- dos命令批处理发送文字到剪贴板
方法一(推荐): echo 要发送到剪贴板的字 | clip 方法二: @echo off::显示要发送的文字重定向到windows临时文件夹下echo 要发送到剪贴板的字>%system ...
- xdoj-1149(多重集合+容斥原理+组合数取模)
#include <iostream> #include <algorithm> #include <cstdio> using namespace std; ty ...
- LeetCode-Microsoft-Remove K Digits
Given a non-negative integer num represented as a string, remove k digits from the number so that th ...
- stenciljs 学习十二 官方doc 路由使用的例子
路由在单页面应用开发中是一个相对比较重要的位置 以下为官方网站的路由配置 <stencil-router scrollTopOffset={0}> <stencil-route-sw ...
- openwrt lamp
https://applefreak111.wordpress.com/2013/03/12/howtoopenwrt-lamp-stack%E5%AE%89%E8%A3%9D/ opkg updat ...
- vuex、redux、mobx 对比
出处:https://www.w3cplus.com/javascript/talk-about-front-end-state-management.html 其实大部分概念都差不多,只不过VUEX ...
- spring 注解列表
table th:first-of-type { width: 15%; } table th:nth-of-type(2) { } 注解 作用 例子 @SuppressWarnings 忽略警告 类 ...
- hadoop零基础入门之DKH安装准备
前几天去参加了一个线下的聚会,参加聚会的基本都是从事互联网工作的.会上有人提到了区块链,从而引发了一场关于大数据方面的探讨.我也是从去年才正式接触大数据,一直在学习hadoop.相信接触过hadoop ...
- 【Oracle学习笔记-5--】集合操作之union,intersect和minus操作
--union并操作 select e.employee_id,e.last_name from hr.employees e where e.last_name like 'C%' union se ...
- maven学习(6)-Maven依赖范围
一.maven依赖范围: classpath 分为三种:编译classpath , 测试classpath , 运行classpath Scope 选项如下: Compile:编译依赖范围.默认就是c ...