【Python爬虫基础】抓取知乎页面所有图片
抓取地址所有图片
#! /usr/bin/env python
from urlparse import urlsplit
from os.path import basename
import urllib2
import re
import requests
import os
import json url = 'https://www.zhihu.com/question/37787176' if not os.path.exists('images'):
os.mkdir("images") print("start>>>>>>>") page_size = 50
offset = 0
url_content = urllib2.urlopen(url).read()
answers = re.findall('h3 data-num="(.*?)"', url_content)
limits = int(answers[0]) while offset < limits:
post_url = "http://www.zhihu.com/node/QuestionAnswerListV2"
params = json.dumps({
'url_token': 37787176,
'pagesize': page_size,
'offset': offset
})
data = {
'_xsrf': '',
'method': 'next',
'params': params
}
header = {
'User-Agent': "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:34.0) Gecko/20100101 Firefox/34.0",
'Host': "www.zhihu.com",
'Referer': url
}
response = requests.post(post_url, data=data, headers=header)
answer_list = response.json()["msg"]
img_urls = re.findall('img .*?src="(.*?_b.*?)"', ''.join(answer_list))
for img_url in img_urls:
try:
img_data = urllib2.urlopen(img_url).read()
file_name = basename(urlsplit(img_url)[2])
print(file_name)
output = open('images/' + file_name, 'wb')
output.write(img_data)
output.close()
except:
pass
offset += page_size print("end>>>>>>>")
正则抓取网页title
#!/usr/bin/python
# coding:utf-8
import httplib2
import urllib2
import re #正则表达式模块 class PageClass:
#获取指定url的网页内容
def get_page(self,url,headers):
http=httplib2.Http()
response,content=http.request(url,'GET',headers=headers)
return content.decode('utf-8') def main():
headers={"cookie":'your cookie'}
url = 'http://v.ktgj.com'
#print headers
page = PageClass()
content = page.get_page(url,headers)
return content if __name__ == "__main__":
htmltext = main()
pattern = re.compile(r'<title>(.*?)</title>')
match = pattern.match(htmltext)
if match:
print match.group()
print htmltext
下载网页图片
#! /usr/bin/env python
from urlparse import urlsplit
from os.path import basename
import urllib2
import re
import requests
import os
import json
import datetime if not os.path.exists('images'):
os.mkdir("images") print("start>>>>>>>>>>>>>>>>>>>>>>>") url = "http://www.ssff66.com/se/jingpintaotu/519271.html"
response = requests.get(url)
#print(response.text)
img_urls = re.findall('img .*?src="(.*?)"', response.text)
#print(img_urls) for img_url in img_urls:
try:
img_data = urllib2.urlopen(img_url,timeout = 5).read()
file_name = basename(urlsplit(img_url)[2])
print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + " " + file_name)
output = open('images/' + file_name, 'wb')
output.write(img_data)
output.close()
except Exception,e:
print("error : " + e.message)
pass print("end>>>>>>>>>>>>>>>>>>>>>>>")
【Python爬虫基础】抓取知乎页面所有图片的更多相关文章
- Python爬虫实战---抓取图书馆借阅信息
Python爬虫实战---抓取图书馆借阅信息 原创作品,引用请表明出处:Python爬虫实战---抓取图书馆借阅信息 前段时间在图书馆借了很多书,借得多了就容易忘记每本书的应还日期,老是担心自己会违约 ...
- 【转】Python爬虫:抓取新浪新闻数据
案例一 抓取对象: 新浪国内新闻(http://news.sina.com.cn/china/),该列表中的标题名称.时间.链接. 完整代码: from bs4 import BeautifulSou ...
- Python爬虫:抓取新浪新闻数据
案例一 抓取对象: 新浪国内新闻(http://news.sina.com.cn/china/),该列表中的标题名称.时间.链接. 完整代码: from bs4 import BeautifulSou ...
- Python爬虫实现抓取腾讯视频所有电影【实战必学】
2019-06-27 23:51:51 阅读数 407 收藏 更多 分类专栏: python爬虫 前言本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问 ...
- Python爬虫,抓取淘宝商品评论内容!
作为一个资深吃货,网购各种零食是很频繁的,但是能否在浩瀚的商品库中找到合适的东西,就只能参考评论了!今天给大家分享用python做个抓取淘宝商品评论的小爬虫! 思路 我们就拿"德州扒鸡&qu ...
- python爬虫数据抓取方法汇总
概要:利用python进行web数据抓取方法和实现. 1.python进行网页数据抓取有两种方式:一种是直接依据url链接来拼接使用get方法得到内容,一种是构建post请求改变对应参数来获得web返 ...
- Python爬虫:抓取手机APP的数据
摘要 大多数APP里面返回的是json格式数据,或者一堆加密过的数据 .这里以超级课程表APP为例,抓取超级课程表里用户发的话题. 1.抓取APP数据包 表单: 表单中包括了用户名和密码,当然都是加密 ...
- python爬虫批量抓取ip代理
使用爬虫抓取数据时,经常要用到多个ip代理,防止单个ip访问太过频繁被封禁.ip代理可以从这个网站获取:http://www.xicidaili.com/nn/.因此写一个python程序来获取ip代 ...
- Python爬虫之抓取豆瓣影评数据
脚本功能: 1.访问豆瓣最受欢迎影评页面(http://movie.douban.com/review/best/?start=0),抓取所有影评数据中的标题.作者.影片以及影评信息 2.将抓取的信息 ...
随机推荐
- 一览css布局标准
回顾历史,CSS1于1996.12.17发正式版,它是为辅助HTML的展现效果而生的.1998.5.12,CSS2发正式版.随后发修订版CSS2.1,纠正了CSS2中的一些错误.注意从CSS2起,CS ...
- c - 十六进制 转 十进制
考虑到举一反三,这里顺便上完整代码,顺便可以考虑实现R进制和十进制的转换. 完整代码: #include <stdio.h> #include <math.h> double ...
- Hadoop 停止Job
1.查看所有正在运行的Job Hadoop job -list 2.根据Id停止某一个Job Hadoop job –kill <JobID>
- swift 类 结构体 作为参数 以及可变参数
Class class Person{ var age = 22, name = "frank" func growolder() { self.age++ //++ 要跟住 不要 ...
- web基础--html
WebBasic 1.web应用体系 课程大纲 1.web基础:做网页 2.结构: a.html 勾勒网页结构及内容 b.css ...
- UEFI模式下安装Win 7系统
转载自:http://huayi898.blog.163.com/blog/static/2581351620144442319155/ 下载win7_64bit原版官方系统 1.用软碟通制作U盘启动 ...
- javascript 关于一周前一个月前的处理方法
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Backbone学习笔记
model model的get和set是对model.attributes进行操作,并不是直接对model进行操作 collection collection.set()会触发相应的add,remov ...
- MOOTOOLS简单操作应用知识
在项目中我们经常需要用到全选/反选.等操作按钮. 基于mootools框架与jquery框架不一致.导致缓慢. $('chkall').addEvent('click',function(){ if( ...
- phpcms v9 读取地区联动菜单缓存文件
读取缓存文件的方法是 getcache() 在 phpcms\libs\functions\global.func.php 中可找到. 地区联动菜单的缓存文件是 caches\caches_link ...