Python 爬虫 1 快速入门

时间:2022-05-06
本文章向大家介绍Python 爬虫 1 快速入门,主要内容包括Python 爬虫 快速入门、本文内容:、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

Python 爬虫 快速入门

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

代码:1.crawler-basic.ipynb

本文内容:

  1. 正则表达式
  2. 用正则表达式抓取 html 内容
  3. 半自动爬虫实战:抓取网页上的图片

1. 正则表达式

#-*-coding:utf8-*-

# 导入re,正则表达式库文件
import re
# from re import findall,search,S

secret_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 d
for 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 让 .包括n
s = '''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 f1
print f2

I love

# findall:如果有3个括号,那么元组里就有3个元素。当有第二串满足下面匹配格式时,就会有两个元组
f2 = re.findall('xx(.*?)xx123xx(.*?)xx',s2)
print f2     # f2是个列表list,里面有一个元素且是个元组tuple,这个元组里有两个元素 I 和 love
print 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 output1
print 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('<title>(.*?)</title>',html,re.S).group(1)
print title
极客学院爬虫测试

#爬取链接
links = re.findall('href="(.*?)"',html,re.S)
print links
for each in links:
    print each
    

# 抓取部分文字,先大再小
# 为了避免符合格式,但是不想要的内容去掉,先放大匹配范围
text_field = re.findall('<ul>(.*?)</ul>',html,re.S)[0]
print text_field


# 再在 the_text 中找到文字的格式
the_text = re.findall('">(.*?)</a>',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_link


for 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 re
import requests

# 读取源代码文件:手动把目标网页的源代码copy到txt里,读文件,赋值给变量 html
f = open('source.txt','r')
html = f.read()
f.close()

#匹配图片网址
pic_url = re.findall('img src="(.*?)" class="lessonimg"',html,re.S)
i = 0
print pic_url


for each in pic_url:
    print each


for 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