1. 1python爬取贴吧壁纸
  2.  
  3. 1.1、获取整个页面数据
  4.  
  5. #coding=utf-8
  6. import urllib
  7.  
  8. def getHtml(url):
  9. page = urllib.urlopen(url)
  10. html = page.read()
  11. return html
  12.  
  13. html = getHtml("http://tieba.baidu.com/p/2738151262")
  14.  
  15. print html
  16. 复制代码
  17.  
  18. 1.2、筛选页面中想要的数据
  19.  
  20. import re
  21. import urllib
  22.  
  23. def getHtml(url):
  24. page = urllib.urlopen(url)
  25. html = page.read()
  26. return html
  27.  
  28. def getImg(html):
  29. reg = r'src="(.+?\.jpg)" '
  30. imgre = re.compile(reg)
  31. imglist = re.findall(imgre,html)
  32. return imglist
  33.  
  34. html = getHtml("http://tieba.baidu.com/p/2460150866")
  35. print getImg(html)
  36.  
  37. 1.3、将页面筛选的数据保存到本地
  38.  
  39. #coding=utf-8
  40. import urllib
  41. import re
  42.  
  43. def getHtml(url):
  44. page = urllib.urlopen(url)
  45. html = page.read()
  46. return html
  47.  
  48. def getImg(html):
  49. reg = r'src="(.+?\.jpg)" '
  50. imgre = re.compile(reg)
  51. imglist = re.findall(imgre,html)
  52. x = 0
  53. for imgurl in imglist:
  54. urllib.urlretrieve(imgurl,'%s.jpg' % x)
  55. x+=1
  56.  
  57. html = getHtml("http://tieba.baidu.com/p/2460150866")
  58.  
  59. print getImg(html)
  60.  
  61. 抓取昵图网图片 --修改版
  62.  
  63. #coding=utf-8
  64. import urllib
  65. import re
  66.  
  67. def getHtml(url):
  68. page = urllib.urlopen(url)
  69. html = page.read()
  70. return html
  71.  
  72. def getImg(html):
  73. reg = r'src="(.*?)" '
  74. imgre = re.compile(reg)
  75. imglist = re.findall(imgre,html)
  76. x = 0
  77. for imgurl in imglist:
  78. urllib.urlretrieve(imgurl,'D:360\\%s.jpg' % x)
  79. x+=1
  80.  
  81. html = getHtml("http://www.nipic.com/show/17742538.html")
  82.  
  83. print getImg(html)
  84.  
  85. 解释:
  86.  
  87. %s意思是字符串参数,就是将变量的值传入到字符串里面,字符串后的'%'后就是写要传入的参数。
  88. 在你给出的例子中,就是用x的值替代%s。比如说x=5,那么就是爬取url后面是'5.jpg'这个图片
  89.  
  90. 保存的位置默认为程序的存放目录
  91.  
  92. 如何保存到指定目录:urllib.urlretrieve(imgurl,'D:360\\%s.jpg' % x)
  93.  
  94. https://image.baidu.com/search/detail?ct=503316480&z=0&ipn=false&word
  95.  
  96. 2python抓取价格
  97.  
  98. 前两个不用加 text
  99.  
  100. #-*—coding:utf8-*-
  101. from lxml import etree
  102.  
  103. import urllib
  104. import urllib.request
  105. #headers构造一个字典,里面保存了user-agent
  106. #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' }
  107. url="http://search.jd.com/Search?keyword=%E6%89%8B%E6%9C%BA&enc=utf-8&pvid=194960f41c994e81ada43edbc276f54b"
  108. html = urllib.request.urlopen(url).read()
  109. data=html.decode('utf-8')
  110. selector = etree.HTML(data)
  111. #xpath
  112. qiubai_text = selector.xpath('//div/ul/li/div/div/strong/i/text()')
  113. #print(qiubai_text)
  114. for i in qiubai_text:
  115. print(i)
  116.  
  117. 或者
  118.  
  119. #-*—coding:utf8-*-
  120. from lxml import etree
  121.  
  122. import urllib
  123. import urllib.request
  124. #headers构造一个字典,里面保存了user-agent
  125. #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' }
  126. url="http://search.jd.com/Search?keyword=%E6%89%8B%E6%9C%BA&enc=utf-8&pvid=194960f41c994e81ada43edbc276f54b"
  127. html = urllib.request.urlopen(url).read()
  128. selector = etree.HTML(html)
  129. #xpath
  130. qiubai_text = selector.xpath('//div/ul/li/div/div/strong/i/text()')
  131. #print(qiubai_text)
  132. for i in qiubai_text:
  133. print(i)
  134.  
  135. 或者 :注意:这个需要加text html.text
  136.  
  137. #-*—coding:utf8-*-
  138. from lxml import etree
  139. import requests
  140. #headers构造一个字典,里面保存了user-agent
  141. #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' }
  142. url="http://search.jd.com/Search?keyword=%E6%89%8B%E6%9C%BA&enc=utf-8&pvid=194960f41c994e81ada43edbc276f54b"
  143. html = requests.get(url)
  144. selector = etree.HTML(html.text)
  145. #xpath
  146. qiubai_text = selector.xpath('//div/ul/li/div/div/strong/i/text()')
  147. #print(qiubai_text)
  148. for i in qiubai_text:
  149. print(i)
  150.  
  151. 3python爬取昵图网图片
  152.  
  153. #coding=utf-8
  154. import urllib
  155. import re
  156.  
  157. def getHtml(url):
  158. page = urllib.urlopen(url)
  159. html = page.read()
  160. return html
  161.  
  162. def getImg(html):
  163. reg = r'src="(.*?)" '
  164. imgre = re.compile(reg)
  165. imglist = re.findall(imgre,html)
  166. x = 0
  167. for imgurl in imglist:
  168. urllib.urlretrieve(imgurl,'D:360\\%s.jpg' % x)
  169. x+=1
  170.  
  171. html = getHtml("http://www.nipic.com/show/17742538.html")
  172.  
  173. print getImg(html)
  174.  
  175. 4、爬音乐
  176.  
  177. # coding:utf-8
  178. import urllib
  179. import urllib.request
  180. import re
  181. url="http://www.yy8844.cn/ting/ccceo/ceeivi.shtml"
  182. html = urllib.request.urlopen(url).read()
  183. data=html.decode('GBK')
  184. #print(data)
  185. music_id = int(re.findall(r'MusicId=(\d+)',data)[0])
  186. music_name = re.findall(r'<title>(.*?)</title>',data)[0].split('/')[0].strip()
  187. music_word = re.findall(r'<div class="textgeci_show" id="showtext">(.*?)</div>',data,re.S)[0]
  188. article='word'
  189. with open("%s.txt" % article,'w') as f:
  190. f.write(music_word)
  191. #print(music_word)
  192. quanurl="http://96.ierge.cn/"'%d/%d/%s' % (music_id//30000,music_id//2000,music_id)+".mp3"
  193. #print(quanurl)
  194. bata=urllib.request.urlopen(quanurl).read()
  195. with open("%s.mp3" % music_name,'wb') as f:
  196. f.write(bata)
  197.  
  198. 注意问题:
  199.  
  200. music_word = re.findall(r'<div class="textgeci_show" id="showtext">(.*?)</div>',data,re.S)[0]
  201.  
  202. pythonAttributeError解决
  203.  
  204. Python 脚本报错】AttributeError:'module' has no attribute 'xxx'的解决方法
  205. http://blog.csdn.net/cn_wk/article/details/50839159
  206.  
  207. int库的.pyc文件
  208.  
  209. python 去掉 .pyc
  210. http://blog.csdn.net/ubuntu64fan/article/details/48241985
  211.  
  212. python操作对象属性
  213. http://www.jianshu.com/p/c38a81b8cb38
  214.  
  215. Python学习日记4|python爬虫常见报错小结及解决方法
  216.  
  217. http://www.jianshu.com/p/17c921639ad0
  218.  
  219. #coding=utf-8
  220. from Tkinter import *
  221. import tkMessageBox
  222. import urllib
  223. import json
  224. import mp3play
  225. import time
  226. import threading
  227. from pinyin import PinYin
  228. import os
  229. import stat
  230. test = PinYin()
  231. test.load_word()
  232. stop=0
  233. def music():
  234. if not entry.get():
  235. tkMessageBox.showinfo("温馨提示","搜索内容不能为空")
  236. return
  237. name = test.hanzi2pinyin_split(entry.get())
  238. html=urllib.urlopen("http://s.music.163.com/search/get/?type=1&s=%s&limit=9"%name).read()
  239. js=json.loads(html)
  240. n = 0
  241. global x
  242. x = []
  243. for i in js['result']['songs']:
  244. listbox.insert(n,'%s(%s)'%(i['name'],i['artists'][0]['name']))
  245. n+=1
  246. x.append(i['audio'])
  247. count = 0
  248. #isplaying = None
  249. def play():
  250. global count
  251. count += 1
  252. index=listbox.curselection()
  253. var1.set(u"正在加载"+listbox.get(index,last=None))
  254. urllib.urlretrieve(x[index[0]],'tmp%s.mp3'%str(count))
  255. var1.set(u"正在播放"+listbox.get(index,last=None))
  256. mp3=mp3play.load("tmp%s.mp3"%str(count))
  257. mp3.play()
  258. time.sleep(mp3.seconds())
  259.  
  260. import inspect
  261. import ctypes
  262.  
  263. def _async_raise(tid, exctype):
  264. """raises the exception, performs cleanup if needed"""
  265. tid = ctypes.c_long(tid)
  266. if not inspect.isclass(exctype):
  267. exctype = type(exctype)
  268. res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype))
  269. if res == 0:
  270. raise ValueError("invalid thread id")
  271. elif res != 1:
  272. ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None)
  273. raise SystemError("PyThreadState_SetAsyncExc failed")
  274.  
  275. def stop_thread(thread):
  276. _async_raise(thread.ident, SystemExit)
  277. threads=list()
  278. t=None
  279. def excute(event):
  280. global t
  281. for i in threads:
  282. stop_thread(i)
  283. t = threading.Thread(target=play)
  284. t.setDaemon(True)
  285. t.start()
  286. threads.append(t)
  287. root = Tk()#创建一个窗口
  288. root.title("云音乐")
  289. root.geometry("500x300+500+200")
  290. entry=Entry(root)#创建输入框(单行),置父
  291. entry.pack()
  292. btn=Button(root,text="搜 索",command=music)
  293. btn.pack()#布局方式必须用同一种
  294. var=StringVar()
  295. listbox=Listbox(root,width=50,listvariable=var)
  296. listbox.bind('<Double-Button-1>',excute)
  297. listbox.pack()
  298. var1=StringVar()
  299. label=Label(root,text="云音乐播放器",fg="purple",textvariable=var1)
  300. var1.set("云音乐播放器")
  301. label.pack()
  302. root.mainloop()#显示窗口

python爬虫小实例的更多相关文章

  1. Python_爬虫小实例

    爬虫小实例 一.问题描述与分析 Q:查询某一只股票,在百度搜索页面的结果的个数以及搜索结果的变化. 分析: 搜索结果个数如下图: 搜索结果的变化:通过观察可以看到,每个一段时间搜索结果的个数是有所变化 ...

  2. 一个python爬虫小程序

    起因 深夜忽然想下载一点电子书来扩充一下kindle,就想起来python学得太浅,什么“装饰器”啊.“多线程”啊都没有学到. 想到廖雪峰大神的python教程很经典.很著名.就想找找有木有pdf版的 ...

  3. 适合新手的Python爬虫小程序

    介绍:此程序是使用python做的一个爬虫小程序  爬取了python百度百科中的部分内容,因为这个demo是根据网站中的静态结构爬取的,所以如果百度百科词条的html结构发生变化 需要修改部分内容. ...

  4. Python 入门小实例笔记

    实例1:打印用户输入的姓名与手机号码知识点:编码,获取输入,变量,标准输出 #encoding=utf-8 import time #1.提示用户输入信息 name = input ("请输 ...

  5. Python 爬虫入门实例(爬取小米应用商店的top应用apk)

    一,爬虫是什么? 爬虫就是获取网络上各种资源,数据的一种工具.具体的可以自行百度. 二,如何写简单爬虫 1,获取网页内容 可以通过 Python(3.x) 自带的 urllib,来实现网页内容的下载. ...

  6. 找python爬虫小项目?github给你准备好了!

    前言 即使我们都是程序员,但我们也并非都会修电脑,都会做酷炫的ppt,都会优化系统卡顿.其实程序员也是分行业.分专业的,就像医生也分内外科.呼吸科.神经科神的. 作为非专业的python选手,或者非专 ...

  7. 10个python爬虫入门实例

    昨天和伙伴萌一块学习,写了几个简单的入门实例 涉及主要知识点: web是如何交互的 requests库的get.post函数的应用 response对象的相关函数,属性 python文件的打开,保存 ...

  8. python: DOM 小实例

    一.全选 全部取消  反选 全选:选择指定的所有项目. 全部取消: 取消所有选定的项目. 反选: 选择未选定的,之前已选定的则取消. <!DOCTYPE html> <html la ...

  9. Python爬虫小实践:爬取任意CSDN博客所有文章的文字内容(或可改写为保存其他的元素),间接增加博客访问量

    Python并不是我的主业,当初学Python主要是为了学爬虫,以为自己觉得能够从网上爬东西是一件非常神奇又是一件非常有用的事情,因为我们可以获取一些方面的数据或者其他的东西,反正各有用处. 这两天闲 ...

随机推荐

  1. memset初始化数组的坑

    memset函数常被我们用来初始化数组,然而有个坑可能会被我们踩到. 静态数组初始化 一般情形是这样的: #include <cstring> int main() { // 静态数组ar ...

  2. 转 shell中的多进程【并发】

    原文地址https://bbs.51cto.com/thread-1104907-1-1.html 根据我个人的理解, 所谓的多进程 只不过是将多个任务放到后台执行而已,很多人都用到过,所以现在讲的主 ...

  3. 修改Jenkins目录

    注意:在Jenkins运行时是不能更改的. 请先将Jenkins停止运行. 1.windows环境下更改JENKINS的主目录 Windows环境中,Jenkins主目录默认在C:\Documents ...

  4. BZOJ1821 部落划分[最小生成树]

    方法一:套路性的,二分距离,然后把距离点对距离小于答案的边都联通起来,然后看集合数量超过k说明答案小,增大,否则减小. 方法二:贪心,类kruskal.n个点,k个连通块,则需要有效连接(同一个块内的 ...

  5. vue组件开发练习--焦点图切换

    1.前言 vue用了有一段时间了,开发的后台管理系统也趋于完善,现在时间比较算是有点空闲吧!这个空闲时间我在研究vue的另外的一些玩法,比如组件,插件等.今天,我就分享一个组件的练手项目--焦点图切换 ...

  6. python中ord()函数,chr()函数,unichr()函数

    ord()函数,chr()函数,unichr()函数 chr()函数用一个范围在range(256)内的(就是0-255)整数作参数,返回一个对应的字符.unichr()跟它一样,只不过返回的是Uni ...

  7. 如何获取到一个form中的所有子控件?

    使用yield关键字,非常的方便 private static IEnumerable<Control> GetChildren(Control frmRootDock) { if (fr ...

  8. vue3.0以上关于打包后出现空白页和路由不起作用

    1.解决页面空白,找不到资源 在项目根目录中的vue.config.js中publicPath: '/'修改为publicPath: './',如果没有这个文件,新建一个,基础代码为: module. ...

  9. oracle rowtype

    v_customer customerinfo%rowtype; select * into v_customer from customerinfo where guid = v_loan.cust ...

  10. react-native-swiper的Github地址

    https://github.com/liyinglihuannan/react-native-swiper https://www.jianshu.com/p/4dba338ef37a(中文版