栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python 爬虫 1 快速入门

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python 爬虫 1 快速入门

Python 爬虫 快速入门

参考资料:极客学院: Python定向爬虫

代码:1.crawler-basic.ipynb

本文内容:
  1. 正则表达式

  2. 用正则表达式抓取 html 内容

  3. 半自动爬虫实战:抓取网页上的图片

1. 正则表达式

#-*-coding:utf8-*-# 导入re,正则表达式库文件import re# from re import findall,search,Ssecret_code = 'hadkfalifexxIxxfasdjifja134xxlovexx23345sdfxxyouxx8dfse'print secret_code

hadkfalifexxIxxfasdjifja134xxlovexx23345sdfxxyouxx8dfse

# .的使用举例:.就是占位符,几个 . 就是几个符号# 在 a 中找 x.a = 'xy123'b1 = re.findall('x.',a)print b1

b2 = re.findall('x..',a)print b2

['xy']
['xy1']

# *的使用举例:* 可以匹配前一个字符 0 次或者 无数次a = 'xyxyxxx123'b = re.findall('x*',a)print b

['x', '', 'x', '', 'xxx', '', '', '', '']

# ?的使用举例:? 可以匹配前一个字符 0 次或者 1次a = 'xyxyxxx123'b = re.findall('x?',a)print b

['x', '', 'x', '', 'x', 'x', 'x', '', '', '', '']

'''上面的内容全部都是只需要了解即可,需要掌握的只有下面这一种组合方式(.*?)'''# .*的使用举例:.* 像个胖子,能吃多少吃多少,从第一个xx到最后一个xx只要满足条件就都吃掉# secret_code = 'hadkfalifexxIxxfasdjifja134xxlovexx23345sdfxxyouxx8dfse'b = re.findall('xx.*xx',secret_code)print b

['xxIxxfasdjifja134xxlovexx23345sdfxxyouxx']

# .*?的使用举例:.*?像个婴儿,少量多餐,只要满足xx~xx就可以,找到尽量多的满足条件的组合c = re.findall('xx.*?xx',secret_code)print c

['xxIxx', 'xxlovexx', 'xxyouxx']

# #使用括号与不使用括号的差别:需要的内容放在括号里面,不需要的放在括号外面d = re.findall('xx(.*?)xx',secret_code)print dfor each in d:    print each

['I', 'love', 'you']
I
love
you

# .的使用举例:.可以匹配任意字符,但是换行符 n 除外,所以第一行没有找到结尾的xx,第一行信息丢失s = '''sdfxxhello
xxfsdfxxworldxxasdf'''d = re.findall('xx(.*?)xx',s)print d

['fsdf']

# .的使用举例:re.S 让 .包括ns = '''sdfxxhello
xxfsdfxxworldxxasdf'''d = re.findall('xx(.*?)xx',s,re.S)print d

['hellon', 'world']

# 对比findall与search的区别: search 后面的 group(i), 有几个括号,就可以写到几s2 = 'asdfxxIxx123xxlovexxdfd'f1 = re.search('xx(.*?)xx123xx(.*?)xx',s2).group(1)
f2 = re.search('xx(.*?)xx123xx(.*?)xx',s2).group(2)print f1print f2

I
love

# findall:如果有3个括号,那么元组里就有3个元素。当有第二串满足下面匹配格式时,就会有两个元组f2 = re.findall('xx(.*?)xx123xx(.*?)xx',s2)print f2     # f2是个列表list,里面有一个元素且是个元组tuple,这个元组里有两个元素 I 和 loveprint f2[0][1]

[('I', 'love')]
love

# sub的使用举例: 替换 s 中符合匹配规律的地方s = '123abcssfasdfas123'# s = '123rrrrr123'output1 = re.sub('123(.*?)123','123789123',s)
output2 = re.sub('123(.*?)123','123%d123'%789,s)print output1print output2

123789123
123789123

# 匹配数字:更方便的方法匹配出数字a = 'asdfasf1234567fasd555fas'b = re.findall('(d+)',a)print b

['1234567', '555']

2. 用正则表达式抓取 html 内容

# 要抓取的网址,20页面old_url = 'http://www.jikexueyuan.com/course/android/?pageNum=2'total_page = 20# 读取txt的内容放在 html 变量里f = open('text.txt','r')
html = f.read()
f.close()#爬取标题:用 search 因为只要找到一个匹配的就不会再去找了,而findall会一直遍历找到尽可能多的,在确定内容只有一个时,用search省时间title = re.search('(.*?)',html,re.S).group(1)print title
极客学院爬虫测试#爬取链接links = re.findall('href="(.*?)"',html,re.S)print linksfor each in links:    print each    

# 抓取部分文字,先大再小# 为了避免符合格式,但是不想要的内容去掉,先放大匹配范围text_field = re.findall('
    (.*?)
',html,re.S)[0]print text_field# 再在 the_text 中找到文字的格式the_text = re.findall('">(.*?)',text_field,re.S)for every_text in the_text:    print every_text     #sub实现翻页for i in range(2,total_page+1):     new_link = re.sub('pageNum=d+','pageNum=%d'%i,old_url,re.S)    print new_linkfor i in range(2,total_page+1):     new_link = re.sub('pageNum=d+','pageNum=%d'%i,old_url,re.S)    print new_link

3. 半自动爬虫实战:抓取网页上的图片

import reimport requests# 读取源代码文件:手动把目标网页的源代码copy到txt里,读文件,赋值给变量 htmlf = open('source.txt','r')
html = f.read()
f.close()#匹配图片网址pic_url = re.findall('img class="lazyload" src="https://www.mshxw.com/file/upload/202104/14/165833390.png" data-original="(.*?)" class="lessonimg"',html,re.S)
i = 0print pic_urlfor each in pic_url:    print eachfor each in pic_url:    print 'now downloading:' + each
    pic = requests.get(each)                       # 用requests的get方法 下载图片
    fp = open('pic2\' + str(i) + '.jpg','wb')     # 事先建立好文件夹 pic2,保存到本地文件,文件名是 序号.jpg
    fp.write(pic.content)
    fp.close()
    i += 1
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/223802.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号