此脚本用于爬站点的下载链接,最终输出到txt文档中。

如果是没有防盗链设置的站点,也可以使用脚本中的下载函数尝试直接下载。

本脚本是为了短期特定目标设计的,如果使用它爬其它特征的资源链接需自行修改配置语句。

python初学者,请多多指正。

# -*- coding: utf-8 -*-
import re
import urllib
import os
import urllib2
import requests
import time #download the file
def download(page, url):
local_filename =url.split('/')[-1] + page + '.jpg'
r = requests.get(url, stream=True)
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size = 1024):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
f.flush() return local_filename #turn the data array into urls array
def print_urls(urls):
output_urls = []
for link in urls:
start_link = link.find('"')
end_link = link.find('"', start_link+1)
output_link = link[start_link+1: end_link]
if output_link.find('http') == -1:
output_link = 'http://www.XXX.com' + output_link
if link.count('"') > 2:
continue
else:
output_urls.append(output_link)
return output_urls def output_download_link_page(page):
url = page
s = urllib.urlopen(url).read()
urls = []
img_urls = 'no image on' + page
new_stl_urls = [] title = re.findall(r'<h1>.+<\/h1>', s, re.I)
if len(title) != 0:
title = title[0]
else:
title = 'no title' img_urls = print_urls(re.findall(r'href=".*?\.jpg.*?"', s, re.I))
if len(img_urls) != 0:
img_urls = img_urls[0]
else:
img_urls = 'no image' + page stl_urls = print_urls (set(re.findall(r'href="/download/.*?"', s, re.I))) for url in stl_urls:
#url = urllib2.urlopen(url).url
url = requests.get(url).url
new_stl_urls.append(url) urls.append(title)
urls.append(img_urls)
urls = urls + new_stl_urls return urls #print output_download_link_page('http://www.XXX.com/thing/46876') #output all links to download
def output_all_pages(site):
s = urllib.urlopen(site).read()
page = re.findall(r'href="/thing/.*?"', s, re.I)
page = set(page)
return print_urls(page) #output all the sites to download
def generate_sites(start, end):
sites = []
for num in range(start, end):
sites.append('http://www.XXX.com/popular?query=&pg=' + str(num))
return sites #write all the results to a txt file
file_new = open ('1.txt', 'r+')
url_pakage = []
sites = generate_sites(40, 46)
count = 0 for site in sites:
print site
file_new.write( '\n' + site)
pages = output_all_pages(site)
for page in pages:
urls = output_download_link_page(page)
#
if len(urls) >= 10:
continue
count = count + 1
for url in urls:
file_new.write(url + '\n')
print 'done'
time.sleep(10) file_new.close()
print 'all done. all..' + str(count) + '..models'

  

  

python 简易小爬虫的更多相关文章

  1. 亲身试用python简单小爬虫

    前几天基友分享了一个贴吧网页,有很多漂亮的图片,想到前段时间学习的python简单爬虫,刚好可以实践一下. 以下是网上很容易搜到的一种方法: #coding=utf-8 import urllib i ...

  2. python的小爬虫的基本写法

    1.最基本的抓站 import urllib2 content = urllib2.urlopen('http://XXXX').read() 2.使用代理服务器 这在某些情况下比较有用,比如IP被封 ...

  3. python简单小爬虫爬取易车网图片

    上代码: import requests,urllib.request from bs4 import BeautifulSoup url = 'http://photo.bitauto.com/' ...

  4. python图片小爬虫

    import re import urllib import os def rename(name): name = name + '.jpg' return name def getHtml(url ...

  5. Python练习,网络小爬虫(初级)

    最近还在看Python版的rcnn代码,附带练习Python编程写一个小的网络爬虫程序. 抓取网页的过程其实和读者平时使用IE浏览器浏览网页的道理是一样的.比如说你在浏览器的地址栏中输入    www ...

  6. python简易爬虫来实现自动图片下载

    菜鸟新人刚刚入住博客园,先发个之前写的简易爬虫的实现吧,水平有限请轻喷. 估计利用python实现爬虫的程序网上已经有太多了,不过新人用来练手学习python确实是个不错的选择.本人借鉴网上的部分实现 ...

  7. python 10 min系列三之小爬虫(一)

    python10min系列之小爬虫 前一篇可视化大家表示有点难,写点简单的把,比如命令行里看论坛的十大,大家也可以扩展为抓博客园的首页文章 本文原创,同步发布在我的github上 据说去github右 ...

  8. Python 基于学习 网络小爬虫

    <span style="font-size:18px;"># # 百度贴吧图片网络小爬虫 # import re import urllib def getHtml( ...

  9. Python爬虫01——第一个小爬虫

    Python小爬虫——贴吧图片的爬取 在对Python有了一定的基础学习后,进行贴吧图片抓取小程序的编写. 目标: 首先肯定要实现图片抓取这个基本功能 然后实现对用户所给的链接进行抓取 最后要有一定的 ...

随机推荐

  1. B. Equal Rectangles

    B. Equal Rectangles 给定4*N个数,是否能构成N个矩形 面积均相等 每次取两个大的,两个小的 #include<bits/stdc++.h> using namespa ...

  2. js中获取当前系统时间

    使用var myDate = new Date();//获取系统当前时间 获取特定格式的时间: 1 myDate.getYear(); //获取当前年份(2位) 2 myDate.getFullYea ...

  3. UVALive 6858 Frame (模拟)

    Frame 题目链接: http://acm.hust.edu.cn/vjudge/contest/130303#problem/D Description http://7xjob4.com1.z0 ...

  4. canvas万花筒案例

    <!DOCTYPE html><html><head> <meta charset="UTF-8"> <title>Ti ...

  5. Ubuntu 系统搭建LNMP环境

    当前Linux版本:Ubuntu16.04 一.安装Nginx 在终端中输入命令 " sudo apt-get install nginx ",在确认安装完成后,在浏览器中访问 l ...

  6. C/C++判断字符串是否包含某个子字符串

    C风格 #include <iostream> #include <string> #include <cstring> using namespace std; ...

  7. Vue引入Jquery和Bootstrap

    一.引入jquery包 npm i jquery 二.配置jquery 在webpack.base.conf.js中加载juery插件  所以要配置该文件 三.引入Bootstrap npm i bo ...

  8. EntityFrameworkCore.MySql

    1.点击“工具”->“NuGet包管理器”->“程序包管理器控制台” 分别安装以下几个包 Mysql 版本: Install-Package MySql.Data.EntityFramew ...

  9. Java ——Scanner

    本节重点思维导图 import java.util.Scanner;//导包 public class Demo { public static void main(String[] args) { ...

  10. lambda表达式(2)

    转:http://www.cnblogs.com/kingmoon/archive/2011/05/03/2035696.html "Lambda表达式"是一个匿名函数,是一种高效 ...