python爬虫小实例
1、python爬取贴吧壁纸 1.1、获取整个页面数据 #coding=utf-8
import urllib def getHtml(url):
page = urllib.urlopen(url)
html = page.read()
return html html = getHtml("http://tieba.baidu.com/p/2738151262") print html
复制代码 1.2、筛选页面中想要的数据 import re
import urllib def getHtml(url):
page = urllib.urlopen(url)
html = page.read()
return html def getImg(html):
reg = r'src="(.+?\.jpg)" '
imgre = re.compile(reg)
imglist = re.findall(imgre,html)
return imglist html = getHtml("http://tieba.baidu.com/p/2460150866")
print getImg(html) 1.3、将页面筛选的数据保存到本地 #coding=utf-8
import urllib
import re def getHtml(url):
page = urllib.urlopen(url)
html = page.read()
return html def getImg(html):
reg = r'src="(.+?\.jpg)" '
imgre = re.compile(reg)
imglist = re.findall(imgre,html)
x = 0
for imgurl in imglist:
urllib.urlretrieve(imgurl,'%s.jpg' % x)
x+=1 html = getHtml("http://tieba.baidu.com/p/2460150866") print getImg(html) 抓取昵图网图片 --修改版 #coding=utf-8
import urllib
import re def getHtml(url):
page = urllib.urlopen(url)
html = page.read()
return html def getImg(html):
reg = r'src="(.*?)" '
imgre = re.compile(reg)
imglist = re.findall(imgre,html)
x = 0
for imgurl in imglist:
urllib.urlretrieve(imgurl,'D:360\\%s.jpg' % x)
x+=1 html = getHtml("http://www.nipic.com/show/17742538.html") print getImg(html) 解释: %s意思是字符串参数,就是将变量的值传入到字符串里面,字符串后的'%'后就是写要传入的参数。
在你给出的例子中,就是用x的值替代%s。比如说x=5,那么就是爬取url后面是'5.jpg'这个图片 保存的位置默认为程序的存放目录 如何保存到指定目录:urllib.urlretrieve(imgurl,'D:360\\%s.jpg' % x) https://image.baidu.com/search/detail?ct=503316480&z=0&ipn=false&word 2、python抓取价格 前两个不用加 text #-*—coding:utf8-*-
from lxml import etree import urllib
import urllib.request
#headers构造一个字典,里面保存了user-agent
#headers= { 'User-Agent' : 'User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36' }
url="http://search.jd.com/Search?keyword=%E6%89%8B%E6%9C%BA&enc=utf-8&pvid=194960f41c994e81ada43edbc276f54b"
html = urllib.request.urlopen(url).read()
data=html.decode('utf-8')
selector = etree.HTML(data)
#xpath
qiubai_text = selector.xpath('//div/ul/li/div/div/strong/i/text()')
#print(qiubai_text)
for i in qiubai_text:
print(i) 或者 #-*—coding:utf8-*-
from lxml import etree import urllib
import urllib.request
#headers构造一个字典,里面保存了user-agent
#headers= { 'User-Agent' : 'User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36' }
url="http://search.jd.com/Search?keyword=%E6%89%8B%E6%9C%BA&enc=utf-8&pvid=194960f41c994e81ada43edbc276f54b"
html = urllib.request.urlopen(url).read()
selector = etree.HTML(html)
#xpath
qiubai_text = selector.xpath('//div/ul/li/div/div/strong/i/text()')
#print(qiubai_text)
for i in qiubai_text:
print(i) 或者 :注意:这个需要加text html.text #-*—coding:utf8-*-
from lxml import etree
import requests
#headers构造一个字典,里面保存了user-agent
#headers= { 'User-Agent' : 'User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36' }
url="http://search.jd.com/Search?keyword=%E6%89%8B%E6%9C%BA&enc=utf-8&pvid=194960f41c994e81ada43edbc276f54b"
html = requests.get(url)
selector = etree.HTML(html.text)
#xpath
qiubai_text = selector.xpath('//div/ul/li/div/div/strong/i/text()')
#print(qiubai_text)
for i in qiubai_text:
print(i) 3、python爬取昵图网图片 #coding=utf-8
import urllib
import re def getHtml(url):
page = urllib.urlopen(url)
html = page.read()
return html def getImg(html):
reg = r'src="(.*?)" '
imgre = re.compile(reg)
imglist = re.findall(imgre,html)
x = 0
for imgurl in imglist:
urllib.urlretrieve(imgurl,'D:360\\%s.jpg' % x)
x+=1 html = getHtml("http://www.nipic.com/show/17742538.html") print getImg(html) 4、爬音乐 # coding:utf-8
import urllib
import urllib.request
import re
url="http://www.yy8844.cn/ting/ccceo/ceeivi.shtml"
html = urllib.request.urlopen(url).read()
data=html.decode('GBK')
#print(data)
music_id = int(re.findall(r'MusicId=(\d+)',data)[0])
music_name = re.findall(r'<title>(.*?)</title>',data)[0].split('/')[0].strip()
music_word = re.findall(r'<div class="textgeci_show" id="showtext">(.*?)</div>',data,re.S)[0]
article='word'
with open("%s.txt" % article,'w') as f:
f.write(music_word)
#print(music_word)
quanurl="http://96.ierge.cn/"'%d/%d/%s' % (music_id//30000,music_id//2000,music_id)+".mp3"
#print(quanurl)
bata=urllib.request.urlopen(quanurl).read()
with open("%s.mp3" % music_name,'wb') as f:
f.write(bata) 注意问题: music_word = re.findall(r'<div class="textgeci_show" id="showtext">(.*?)</div>',data,re.S)[0] python中AttributeError解决 【Python 脚本报错】AttributeError:'module' has no attribute 'xxx'的解决方法
http://blog.csdn.net/cn_wk/article/details/50839159 int库的.pyc文件 python 去掉 .pyc
http://blog.csdn.net/ubuntu64fan/article/details/48241985 python操作对象属性
http://www.jianshu.com/p/c38a81b8cb38 Python学习日记4|python爬虫常见报错小结及解决方法 http://www.jianshu.com/p/17c921639ad0 #coding=utf-8
from Tkinter import *
import tkMessageBox
import urllib
import json
import mp3play
import time
import threading
from pinyin import PinYin
import os
import stat
test = PinYin()
test.load_word()
stop=0
def music():
if not entry.get():
tkMessageBox.showinfo("温馨提示","搜索内容不能为空")
return
name = test.hanzi2pinyin_split(entry.get())
html=urllib.urlopen("http://s.music.163.com/search/get/?type=1&s=%s&limit=9"%name).read()
js=json.loads(html)
n = 0
global x
x = []
for i in js['result']['songs']:
listbox.insert(n,'%s(%s)'%(i['name'],i['artists'][0]['name']))
n+=1
x.append(i['audio'])
count = 0
#isplaying = None
def play():
global count
count += 1
index=listbox.curselection()
var1.set(u"正在加载"+listbox.get(index,last=None))
urllib.urlretrieve(x[index[0]],'tmp%s.mp3'%str(count))
var1.set(u"正在播放"+listbox.get(index,last=None))
mp3=mp3play.load("tmp%s.mp3"%str(count))
mp3.play()
time.sleep(mp3.seconds()) import inspect
import ctypes def _async_raise(tid, exctype):
"""raises the exception, performs cleanup if needed"""
tid = ctypes.c_long(tid)
if not inspect.isclass(exctype):
exctype = type(exctype)
res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype))
if res == 0:
raise ValueError("invalid thread id")
elif res != 1:
ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None)
raise SystemError("PyThreadState_SetAsyncExc failed") def stop_thread(thread):
_async_raise(thread.ident, SystemExit)
threads=list()
t=None
def excute(event):
global t
for i in threads:
stop_thread(i)
t = threading.Thread(target=play)
t.setDaemon(True)
t.start()
threads.append(t)
root = Tk()#创建一个窗口
root.title("云音乐")
root.geometry("500x300+500+200")
entry=Entry(root)#创建输入框(单行),置父
entry.pack()
btn=Button(root,text="搜 索",command=music)
btn.pack()#布局方式必须用同一种
var=StringVar()
listbox=Listbox(root,width=50,listvariable=var)
listbox.bind('<Double-Button-1>',excute)
listbox.pack()
var1=StringVar()
label=Label(root,text="云音乐播放器",fg="purple",textvariable=var1)
var1.set("云音乐播放器")
label.pack()
root.mainloop()#显示窗口
python爬虫小实例的更多相关文章
- Python_爬虫小实例
爬虫小实例 一.问题描述与分析 Q:查询某一只股票,在百度搜索页面的结果的个数以及搜索结果的变化. 分析: 搜索结果个数如下图: 搜索结果的变化:通过观察可以看到,每个一段时间搜索结果的个数是有所变化 ...
- 一个python爬虫小程序
起因 深夜忽然想下载一点电子书来扩充一下kindle,就想起来python学得太浅,什么“装饰器”啊.“多线程”啊都没有学到. 想到廖雪峰大神的python教程很经典.很著名.就想找找有木有pdf版的 ...
- 适合新手的Python爬虫小程序
介绍:此程序是使用python做的一个爬虫小程序 爬取了python百度百科中的部分内容,因为这个demo是根据网站中的静态结构爬取的,所以如果百度百科词条的html结构发生变化 需要修改部分内容. ...
- Python 入门小实例笔记
实例1:打印用户输入的姓名与手机号码知识点:编码,获取输入,变量,标准输出 #encoding=utf-8 import time #1.提示用户输入信息 name = input ("请输 ...
- Python 爬虫入门实例(爬取小米应用商店的top应用apk)
一,爬虫是什么? 爬虫就是获取网络上各种资源,数据的一种工具.具体的可以自行百度. 二,如何写简单爬虫 1,获取网页内容 可以通过 Python(3.x) 自带的 urllib,来实现网页内容的下载. ...
- 找python爬虫小项目?github给你准备好了!
前言 即使我们都是程序员,但我们也并非都会修电脑,都会做酷炫的ppt,都会优化系统卡顿.其实程序员也是分行业.分专业的,就像医生也分内外科.呼吸科.神经科神的. 作为非专业的python选手,或者非专 ...
- 10个python爬虫入门实例
昨天和伙伴萌一块学习,写了几个简单的入门实例 涉及主要知识点: web是如何交互的 requests库的get.post函数的应用 response对象的相关函数,属性 python文件的打开,保存 ...
- python: DOM 小实例
一.全选 全部取消 反选 全选:选择指定的所有项目. 全部取消: 取消所有选定的项目. 反选: 选择未选定的,之前已选定的则取消. <!DOCTYPE html> <html la ...
- Python爬虫小实践:爬取任意CSDN博客所有文章的文字内容(或可改写为保存其他的元素),间接增加博客访问量
Python并不是我的主业,当初学Python主要是为了学爬虫,以为自己觉得能够从网上爬东西是一件非常神奇又是一件非常有用的事情,因为我们可以获取一些方面的数据或者其他的东西,反正各有用处. 这两天闲 ...
随机推荐
- 生产者消费者问题--lock
# 代码: public class App { public static void main(String[] args) { Depot depot = new Depot(100); Prod ...
- bug的全部
BUG 的生命周期 BUG 的生命周期 Bug-->软件程序的漏洞或缺陷 Bug 的类型:代码错误.设计缺陷.界面优化.性能问题.配置相关.安装部署.安全相关.标准规划.测试脚本....其他(功 ...
- php 垃圾处理机制
在php5.3版本之前, php变量的回收机制只是简单的通过计数来处理(当refcount=0时,会回收内存),但这样会出现一个问题 $a=array("str"); $a[]=& ...
- IO框架:asyncio 上篇
如何定义/创建协程 只要在一个函数前面加上 async 关键字,这个函数对象是一个协程,通过isinstance函数,它确实是Coroutine类型. from collections.abc imp ...
- 1+X证书试题
2019年下半年Web前端开发中级理论考试 (考试时间9:00-11:30 共150分钟) 1. 本试卷共3道大题,满分100分. 一.单选题(每小题2分,共30小题,共60分) 1.在Bootstr ...
- easypoi 版本依赖关系
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactI ...
- Java-判断是否为回文数
/** * @ClassName: IsPalindrome * @author: bilaisheng * @date: 2017年9月19日 下午2:54:08 * 判断是否为回文数 * true ...
- Hadoop-No.3之序列化存储格式
序列化存储指的是将数据结构转化为字节流的过程,一般用于数据存储或者网络传输.与之相反, 反序列化是将字节流转化为数据结果的过程.序列化是分布处理系统(比如Hadoop)的核心,原因在于他能对数据进行转 ...
- HDU 6043 - KazaQ's Socks | 2017 Multi-University Training Contest 1
/* HDU 6043 - KazaQ's Socks [ 找规律 ] | 2017 Multi-University Training Contest 1 题意: 一共 n 双袜子编号 1~n,每天 ...
- Plx9030通讯卡驱动开发与接口封装
在学校的时候,曾经采用DDK+Driverstudio+VC6.0环境做过9054视频采集卡的驱动开发,回想起调试过程,记得最清楚的就是过无数次的计算机蓝屏重启....今天第一天来到新公司,老大就说你 ...