python requests 正则爬虫
代码:
import requests
from multiprocessing import Pool
from requests.exceptions import RequestException
import re
import json
def get_one_page(url):
try:
if url=='':
return None
response=requests.get(url)
if response.status_code==200:
return response.text
return None
except RequestException:
return None def parse_one_page(html):
if html=='':
return None
pattern=re.compile('<dd>.*?board-index.*?>(\d+)</i>.*?data-src="(.*?)".*?name"><a'
+'.*?>(.*?)</a>.*?star">(.*?)</p>.*?releasetime">(.*?)</p>'
+'.*?integer">(.*?)</i>.*?fraction">(.*?)</i>.*?</dd>',re.S)
items=re.findall(pattern,html)
for item in items:
yield {
'index':item[0],
'image':item[1],
'title':item[2],
'actor':item[3].strip()[3:],
'time':item[4].strip()[5:],
'score':item[5]+item[6]
} def write_to_file(content):
with open('result.txt','a',encoding='utf-8') as f:#a往后追加
f.write(json.dumps(content,ensure_ascii=False)+'\n')
f.close() def main(offset):
url='http://maoyan.com/board/4?offset='+str(offset)
html=get_one_page(url) # 得到html
for item in parse_one_page(html):
print(item)
write_to_file(item) if __name__=='__main__':
# for i in range(10):
# main(i*10)
pool=Pool() #多线程
pool.map(main,[i*10 for i in range(10)])
python requests 正则爬虫的更多相关文章
- python 3.x 爬虫基础---常用第三方库(requests,BeautifulSoup4,selenium,lxml )
python 3.x 爬虫基础 python 3.x 爬虫基础---http headers详解 python 3.x 爬虫基础---Urllib详解 python 3.x 爬虫基础---常用第三方库 ...
- python +requests 爬虫-爬取图片并进行下载到本地
因为写12306抢票脚本需要用到爬虫技术下载验证码并进行定位点击所以这章主要讲解,爬虫,从网页上爬取图片并进行下载到本地 爬虫实现方式: 1.首先选取你需要的抓取的URL:2.将这些URL放入待抓 ...
- Python:requests库、BeautifulSoup4库的基本使用(实现简单的网络爬虫)
Python:requests库.BeautifulSoup4库的基本使用(实现简单的网络爬虫) 一.requests库的基本使用 requests是python语言编写的简单易用的HTTP库,使用起 ...
- python 3.x 爬虫基础---正则表达式
python 3.x 爬虫基础 python 3.x 爬虫基础---http headers详解 python 3.x 爬虫基础---Urllib详解 python 3.x 爬虫基础---Requer ...
- python 3.x 爬虫基础---Requersts,BeautifulSoup4(bs4)
python 3.x 爬虫基础 python 3.x 爬虫基础---http headers详解 python 3.x 爬虫基础---Urllib详解 python 3.x 爬虫基础---Requer ...
- Python学习网络爬虫--转
原文地址:https://github.com/lining0806/PythonSpiderNotes Python学习网络爬虫主要分3个大的版块:抓取,分析,存储 另外,比较常用的爬虫框架Scra ...
- 初探爬虫 ——《python 3 网络爬虫开发实践》读书笔记
零.背景 之前在 node.js 下写过一些爬虫,去做自己的私人网站和工具,但一直没有稍微深入的了解,借着此次公司的新项目,体系的学习下. 本文内容主要侧重介绍爬虫的概念.玩法.策略.不同工具的列举和 ...
- 基于python的pixiv爬虫
基于python的pixiv爬虫 1.目标 在和朋友吹逼过程中,聊到qq群机器人,突发奇想动手做一个p站每日推荐色图的色图机,遂学习爬虫. 目标: 批量下载首页推荐色图. 由于对qq机器人不熟,先利用 ...
- Python 开发轻量级爬虫07
Python 开发轻量级爬虫 (imooc总结07--网页解析器BeautifulSoup) BeautifulSoup下载和安装 使用pip install 安装:在命令行cmd之后输入,pip i ...
随机推荐
- WC2019 游记
Day 0 早上奇迹般的六点半起床平常这时候我还没睡呢 早餐在武汉站吃了一碗28的番茄牛肉米线,结果上菜后我把所有非米线的固体(包括番茄和牛肉)全挑出去了 高二大佬:一个愿宰一个愿挨 在高铁上待了四个 ...
- HardNet解读
论文:Working hard to know your neighbor’s margins: Local descriptor learning loss 为什么介绍此文:这篇2018cvpr文 ...
- AngularJs 刷新页面
第一种: AngularJs 刷新页面可采用下面的方式:首先先在控制器中注册$window,然后定义函数$scope.reloadRoute,在需要刷新页面的地方调用函数$scope.reloadRo ...
- 【Tomcat】tomcat内存配置登记册
20141202: 环境:windows2003 tomcat6.x jdk1.6 启动方式:windows服务方式启动 启动异常:java.lang.OutOfMemoryError: PermGe ...
- 请求Jenkins链接返回403
使用python请求Jenkins链接,返回403 1.使用正确的账号密码(Jenkins -> 系统设置 -> 全局安全设置),该账户拥有访问该Jenkins链接的权限 2.代码中的账号 ...
- ebs 12.1.1升级到12.1.3
升级过程参考 Oracle电子商务套件版本12.1.3自述文件 (文档 ID 1534411.1) 应用启动到维护模式 adadmin 打以下patch 9239089 9239090 92390 ...
- corba/ice/web service/com+
//todo model1 model2
- 【原创】大数据基础之Logstash(3)应用之file解析(grok/ruby/kv)
从nginx日志中进行url解析 /v1/test?param2=v2¶m3=v3&time=2019-03-18%2017%3A34%3A14->{'param1':' ...
- GIT 版本管理-github&码云
GIT 是什么 git是一个用于帮助用户实现版本控制的软件. 把本地代码push到远程服务器 git add.git commitpush code to remove machine.git pus ...
- Python 三种过滤去重方法
SET集合去重 set(1,1,2) REDIS去重 布隆过滤器