抓取地址所有图片

#! /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爬虫基础】抓取知乎页面所有图片的更多相关文章

  1. Python爬虫实战---抓取图书馆借阅信息

    Python爬虫实战---抓取图书馆借阅信息 原创作品,引用请表明出处:Python爬虫实战---抓取图书馆借阅信息 前段时间在图书馆借了很多书,借得多了就容易忘记每本书的应还日期,老是担心自己会违约 ...

  2. 【转】Python爬虫:抓取新浪新闻数据

    案例一 抓取对象: 新浪国内新闻(http://news.sina.com.cn/china/),该列表中的标题名称.时间.链接. 完整代码: from bs4 import BeautifulSou ...

  3. Python爬虫:抓取新浪新闻数据

    案例一 抓取对象: 新浪国内新闻(http://news.sina.com.cn/china/),该列表中的标题名称.时间.链接. 完整代码: from bs4 import BeautifulSou ...

  4. Python爬虫实现抓取腾讯视频所有电影【实战必学】

    2019-06-27 23:51:51 阅读数 407  收藏 更多 分类专栏: python爬虫   前言本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问 ...

  5. Python爬虫,抓取淘宝商品评论内容!

    作为一个资深吃货,网购各种零食是很频繁的,但是能否在浩瀚的商品库中找到合适的东西,就只能参考评论了!今天给大家分享用python做个抓取淘宝商品评论的小爬虫! 思路 我们就拿"德州扒鸡&qu ...

  6. python爬虫数据抓取方法汇总

    概要:利用python进行web数据抓取方法和实现. 1.python进行网页数据抓取有两种方式:一种是直接依据url链接来拼接使用get方法得到内容,一种是构建post请求改变对应参数来获得web返 ...

  7. Python爬虫:抓取手机APP的数据

    摘要 大多数APP里面返回的是json格式数据,或者一堆加密过的数据 .这里以超级课程表APP为例,抓取超级课程表里用户发的话题. 1.抓取APP数据包 表单: 表单中包括了用户名和密码,当然都是加密 ...

  8. python爬虫批量抓取ip代理

    使用爬虫抓取数据时,经常要用到多个ip代理,防止单个ip访问太过频繁被封禁.ip代理可以从这个网站获取:http://www.xicidaili.com/nn/.因此写一个python程序来获取ip代 ...

  9. Python爬虫之抓取豆瓣影评数据

    脚本功能: 1.访问豆瓣最受欢迎影评页面(http://movie.douban.com/review/best/?start=0),抓取所有影评数据中的标题.作者.影片以及影评信息 2.将抓取的信息 ...

随机推荐

  1. Python元组、列表--笔记

    <Python3 程序开发指南> 序列包括元组和列表,首先,我们介绍元组. 元组--tuple 元组为有序的序列,元组和字符串一样也是固定的,不能替换或删除其中的任意数据项.如果需要修改应 ...

  2. poj 1988 Cube Stacking && codevs 1540 银河英雄传说(加权并茶几)

    #include<iostream> #include<cstdio> #include<cstring> #define maxn 30010 using nam ...

  3. ASP.NET-FineUI开发实践-8(二)

    把上回的做一些改进 1.点击grid2的行改变TriggerBox1的值 var v = $(item).find('.x-grid-cell-Name div.x-grid-cell-inner') ...

  4. FastReport 动态修改连接字符串

    代码如下: Report rp = new Report(); rp.Load(@"Print\aa.frx"); rp.Dictionary.Connections[0].Con ...

  5. SpringBoot入门系列:第一篇 Hello World

    跟随SpringBoot的文档(http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-d ...

  6. JS+CSS+HTML简单计算器

    <!doctype html> <html> <head> <title>计算器</title> <meta charset=&quo ...

  7. IO流文件字符输入输出流,缓冲流

    由于字节输入输出流在操纵Unicode字符时可能有乱码现象 于是就有了操作字符的输入输出流 Reader ,Writer和他们的子类FileReader,FileWrite(其实就是用来辅助构造的 W ...

  8. Leetcode 371: Sum of Two Integers(使用位运算实现)

    题目是:Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. ...

  9. 不用修改nginx的高并发合并回源架构

    nginx的连接都是一对一的,想改成一对多,比较麻烦,所以曾经看完了Nginx代码想改成一对多,我还是没改成,后来改变了一下思路想到一个更简单的方案,而且不失并发性能,还容易控制,下面先给出下面的图: ...

  10. xml代码

    修改和删除: <?php$doc=new DOMDocument();$doc->load("php.xml");$root=$doc->documentElem ...