爬虫案例

1.爬取百度首页(模拟用户回车url的操作,下载网页到本地)

#!/usr/bin/python
# -*- coding: UTF-8 -*-
# TODO:实用程序去模拟实现一个功能:把一个网页另存到本地

# 引入url模块来模拟用户回车url的操作,urllib包下面的urlopen函数
from urllib.request import urlopen

# 注意http不要加s,要不有问题
url = "http://www.baidu.com/"
# 模拟打开一个url进行页面访问,response对象,就是发起访问之后得到的响应对象
response = urlopen(url)
# print(response)
# 返回程序发起url请求中的url地址(返回地址不一定和传入的一样,服务器内部转发)
print(response.geturl())
# 返回状态码 200访问成功     404资源未找到    500 system error
print(response.getcode())
# 返回头信息
print(response.info())
print("------------------------------------")
# 真正的把源码读下来
html = response.read()
# 需要转码要不不是我们想要的源码跟编码
html = html.decode("utf-8")
print(html)  # 到此上面程序跟右键网页查看源码的样子是一样的

或者直接使用下面的方式保存到本地系统

#!/usr/bin/python
# -*- coding: UTF-8 -*-
# TODO:实用程序去模拟实现一个功能:把一个网页另存到本地

# urlretrieve就是从远程服务器去下载东西
from urllib.request import urlretrieve

# 注意http不要加s,要不有问题
url = "http://www.baidu.com/"
urlretrieve(url,filename="G:\\szsmworkspace\\test\\mybaidu.html")

上面两种方法都不能下载网页中的图片(只能爬取网页的源码),使用下面的方式下载图片

# 下载百度logo图片(但是需要F12查看图片的地址)
urlretrieve(url="https://www.baidu.com/img/bd_logo1.png",filename="G:\\szsmworkspace\\test\\mybaidu_logo.png")

2.爬取quotes.toscrape.com 网站的名言

#!/usr/bin/python
# -*- coding: UTF-8 -*-
# TODO:爬取名言网 http://quotes.toscrape.com/

# 引入url模块来模拟用户回车url的操作,urllib包下面的urlopen函数,起别名叫做open
from urllib.request import urlopen as open
# re就是用来做正则的模块,全部导入
import re

url = "http://quotes.toscrape.com/"
response = open(url)
# 读取源码
html = response.read()
# 转化编码
html = html.decode("UTF-8")

# TODO:"---------获取名言-----------"
true_quotes = []  # 用来存储名言最终的结果
"""
    数据提取技术 1:正则 2:BeautifulSoup
    通过F12观察出我们想要的数据结构如下
    <span class="text" itemprop="text">.*</span>
"""
# 返回的是一个list 正则()的作用是去掉外层包裹的Html标签
result = re.findall('<span class="text" itemprop="text">(.*)</span>', html)
for single_result in result:
    # strip从两端开始搜寻,只要发现某个字符在当前这个方法的范围内统统去掉
    new_result = single_result.strip("“”")
    true_quotes.append(new_result)
# TODO:"---------获取作者-----------"
true_authors = []  # 用来存储作者
authors = re.findall('<small class="author" itemprop="author">(.*)</small>', html)
for author in authors:
    true_authors.append(author)
# TODO:"---------获取标签----------"
ture_tag = []  # 用来存储标签
# 方式1:(失败)因为 meta 信息是不可匹配的。匹配不到任何信息
# tagss = re.findall('<meta class="keywords" itemprop="keywords" content="(.*)">', html)
# 方式2:
"""
    # 正则表达式进行匹配的时候有两种模式
    str='aaaaaaabaaaa'
    第一种:贪婪模式
    re.findall('aa+',str)
    结果:['aaaaaaa','aaaa']
    第二种:非贪婪模式
    re.findall('aa+?',str)
    结果:['aa','aa','aa','aa','aa']
    
    需要注意的是在py中的正则 . 不代表(不能匹配)\n \r 这种符号
    所以需要第三个参数re.RegexFlag.DOTALL 代表的意思是 . 可以匹配所有的字符,如果不写则不能匹配到换行符以后的东西
"""
tagss = re.findall('<div class="tags">(.*?)</div>', html, re.RegexFlag.DOTALL)
for tags in tagss:
    tags_temp = re.findall('<a class="tag" href="(.*)">(.*)</a>', tags)
    # print(tags_temp) 打印出来的是元组,例如[('/tag/abilities/page/1/', 'abilities'), ('/tag/choices/page/1/', 'choices')] 我们要取角标1的数据
    tag_t1 = []
    for tag in tags_temp:
        tag_t1.append(tag[1])
    tag_str = ",".join(tag_t1)
    ture_tag.append(tag_str)

# 最终进行循环拼接打印 数据格式:名言    作者    标签1,标签2,标签3
for i in range(10):
    print("\t\t".join([true_quotes[i], true_authors[i], ture_tag[i]]))










猜你喜欢

转载自blog.csdn.net/JENREY/article/details/80908107