1、python爬取贴吧壁纸1.1、获取整个页面数据#coding=utf-8
import urllibdef getHtml(url):page = urllib.urlopen(url)html = page.read()return htmlhtml = getHtml("http://tieba.baidu.com/p/2738151262")print html
复制代码1.2、筛选页面中想要的数据import re
import urllibdef getHtml(url):page = urllib.urlopen(url)html = page.read()return htmldef 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 redef getHtml(url):page = urllib.urlopen(url)html = page.read()return htmldef getImg(html):reg = r'src="(.+?\.jpg)" 'imgre = re.compile(reg)imglist = re.findall(imgre,html)x = 0for imgurl in imglist:urllib.urlretrieve(imgurl,'%s.jpg' % x)x+=1html = getHtml("http://tieba.baidu.com/p/2460150866")print getImg(html)抓取昵图网图片 --修改版#coding=utf-8
import urllib
import redef getHtml(url):page = urllib.urlopen(url)html = page.read()return htmldef getImg(html):reg = r'src="(.*?)" 'imgre = re.compile(reg)imglist = re.findall(imgre,html)x = 0for imgurl in imglist:urllib.urlretrieve(imgurl,'D:360\\%s.jpg' % x)x+=1html = 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&word2、python抓取价格前两个不用加 text#-*—coding:utf8-*-
from lxml import etreeimport 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 etreeimport 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 redef getHtml(url):page = urllib.urlopen(url)html = page.read()return htmldef getImg(html):reg = r'src="(.*?)" 'imgre = re.compile(reg)imglist = re.findall(imgre,html)x = 0for imgurl in imglist:urllib.urlretrieve(imgurl,'D:360\\%s.jpg' % x)x+=1html = 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/50839159int库的.pyc文件python 去掉 .pyc
http://blog.csdn.net/ubuntu64fan/article/details/48241985python操作对象属性
http://www.jianshu.com/p/c38a81b8cb38Python学习日记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("温馨提示","搜索内容不能为空")returnname = 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 = 0global xx = []for i in js['result']['songs']:listbox.insert(n,'%s(%s)'%(i['name'],i['artists'][0]['name']))n+=1x.append(i['audio'])
count = 0
#isplaying = None
def play():global countcount += 1index=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 ctypesdef _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  tfor 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()#显示窗口

转载于:https://www.cnblogs.com/effortsing/p/10049469.html

python爬虫小实例相关推荐

  1. 基于python爬虫的岗位数据分析以拉勾网为例_爬虫小实例-拉勾网数据分析岗位...

    原标题:爬虫小实例-拉勾网数据分析岗位 欢迎关注天善智能 hellobi.com,我们是专注于商业智能BI,大数据,数据分析领域的垂直社区,学习.问答.求职,一站式搞定! 对商业智能BI.大数据分析挖 ...

  2. python爬虫入门实例-Python爬虫天气预报实例详解(小白入门)

    本文研究的主要是Python爬虫天气预报的相关内容,具体介绍如下. 要求是把你所在城市过去一年的历史数据爬出来. 分析网站 我们可以看到,我们需要的天气数据都是放在图表上的,在切换月份的时候,发现只有 ...

  3. python编程入门与案例详解-Python爬虫天气预报实例详解(小白入门)

    本文研究的主要是Python爬虫天气预报的相关内容,具体介绍如下. 这次要爬的站点是这个:http://www.weather.com.cn/forecast/ 要求是把你所在城市过去一年的历史数据爬 ...

  4. python爬虫程序实例-10个python爬虫入门实例

    作者:h3zh1 来源:cnblogs.com/h3zh1/p/12548946.html 今天为大家准备了几个简单的python爬虫入门实例,分享给大家. 涉及主要知识点:web是如何交互的 req ...

  5. python3爬虫入门实例_10个python爬虫入门实例(小结)

    昨天带伙伴萌学习python爬虫,准备了几个简单的入门实例 涉及主要知识点: web是如何交互的 requests库的get.post函数的应用 response对象的相关函数,属性 python文件 ...

  6. 从入门到入土:Python爬虫学习|实例练手|爬取猫眼榜单|Xpath定位标签爬取|代码

    此博客仅用于记录个人学习进度,学识浅薄,若有错误观点欢迎评论区指出.欢迎各位前来交流.(部分材料来源网络,若有侵权,立即删除) 本人博客所有文章纯属学习之用,不涉及商业利益.不合适引用,自当删除! 若 ...

  7. 从入门到入土:Python爬虫学习|实例练手|爬取百度翻译|Selenium出击|绕过反爬机制|

    此博客仅用于记录个人学习进度,学识浅薄,若有错误观点欢迎评论区指出.欢迎各位前来交流.(部分材料来源网络,若有侵权,立即删除) 本人博客所有文章纯属学习之用,不涉及商业利益.不合适引用,自当删除! 若 ...

  8. 从入门到入土:Python爬虫学习|实例练手|爬取新浪新闻搜索指定内容|Xpath定位标签爬取|代码注释详解

    此博客仅用于记录个人学习进度,学识浅薄,若有错误观点欢迎评论区指出.欢迎各位前来交流.(部分材料来源网络,若有侵权,立即删除) 本人博客所有文章纯属学习之用,不涉及商业利益.不合适引用,自当删除! 若 ...

  9. 从入门到入土:Python爬虫学习|实例练手|爬取百度产品列表|Xpath定位标签爬取|代码注释详解

    此博客仅用于记录个人学习进度,学识浅薄,若有错误观点欢迎评论区指出.欢迎各位前来交流.(部分材料来源网络,若有侵权,立即删除) 本人博客所有文章纯属学习之用,不涉及商业利益.不合适引用,自当删除! 若 ...

最新文章

  1. 【多标签文本分类】Initializing neural networks for hierarchical multi-label text classification
  2. MySQL运行状态show status详解
  3. Android系统中标准Intent的使用
  4. background 互联网图片_cssbackground-image和layer-background-image的区别
  5. php如果字符串有1 3 5,Day3-php 字符串1
  6. 大数据就业前景分析的太到位了【附:1T视频资料】
  7. 利用R语言绘制世界航班路线图
  8. 下课拉~~~~~~~~~~~想写就写
  9. 回归分析中自变量取舍、检验及多重共线性处理(VIF)
  10. this指向,防抖函数中的fn.apply(this,arguments)作用
  11. 联想E430使用移动硬盘做系统启动
  12. 18000担粮草和新四军情报
  13. 无代码开发大众化,摆脱Excel轻松管理企业数据
  14. C语言——文件操作及常见问题
  15. 《隐私计算法律适用规则报告》:隐私计算如何助力数据合规
  16. 解决 小程序界面数据不显示问题
  17. Lind.DDD敏捷领域驱动框架~Lind.DDD各层介绍
  18. 用Vue做个最简单的搜索框
  19. JAVA java学习(2)——————java下载安装与环境配置
  20. 信息系统项目管理师试题精选(四)

热门文章

  1. 禁用win10自带的微软输入法!
  2. 信息学奥赛一本通:1196:踩方格
  3. 元宇宙赋能传统产业创新架构
  4. pygame.mask原理及使用pygame.mask实现精准碰撞检测
  5. 第三届中青杯数模本科组问题一———股票选择和投资组合方案(excel、python-Markowitz模型、夏普比率模型)
  6. 电子元器件封装设计规范
  7. 6轴机器人运动学(正解)
  8. JConsole:Java监视与管理控制台
  9. Hbuilder快捷键教程
  10. matlab7如何运行程序,技术员为你示范win7系统运行matlab2010找不到指定的程序的具体方法...