美文网首页
PY爬虫极速学习 (九)简单爬虫编写

PY爬虫极速学习 (九)简单爬虫编写

作者: 罗卡恩 | 来源:发表于2019-12-03 20:48 被阅读0次
image.png
import urllib.request

#不要打印信息太多会卡死
data=urllib.request.urlopen("https://www.csdn.net/").read()
image.png image.png
image.png

然后只有vx二维码了 那就爬二维码吧
我是360浏览器 基本都差不多 F12 然后这样点击可以直接跳到该图片的代码位置
是这个精品直播课的


image.png image.png

然后查看改网页源码
Ctrl+F下面会出现查找
找规律看看有没有重复的 可以只输入前几个字 然后看有没有多个变色的 没有就是唯一

image.png

然后粘贴过去 ” 会格式不对改为' 或者字符串改为'

#自动提取课程页面v群
data=urllib.request.urlopen("https://edu.csdn.net/huiyiCourse/detail/1096").read().decode("utf-8")#改编码格式
#我又看了几个有以下规律
#https://img-bss.csdn.net/201911030053543294.jpeg 
#<img alt="" src="https://img-bss.csdn.net/201911190700587460.jpg" style="height:200px; width:200px">
#<img alt="" src="https://img-bss.csdn.net/201911280638295575.jpg" style="height:200px; width:200px">
#<img alt="" src="https://img-bss.csdn.net/201911030053543294.jpeg" style="height:300px; width:300px">

pat=r'<img alt="" src="(.*?)" style="height:\d*px; width:\d*px" />'
rst=re.compile(pat).findall(data,re.S)
print(rst)
image.png

然后再试一个爬豆瓣的


image.png

那个网页没了 就爬下新书速递的书名和连接


image.png
在这里
image.png
data=urllib.request.urlopen("https://book.douban.com").read().decode("utf-8")#改编码格式
pat=r'''
<div class="title">
                <a class="" href="(.*?)"
                  title=".*?">(.*?)</a>
              </div>
'''
rst=re.compile(pat).findall(data,re.S)
print(rst)
image.png

恭喜触发彩蛋


image.png

然后根据网上说要加个head避过爬虫
打开要爬的网页F12 F5 根据箭头依次找到User-Agent


image.png
User_Agent='''
Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36
'''
data=urllib.request.urlopen("https://book.douban.com",User_Agent).read().decode("utf-8")#改编码格式
pat=r'''
<div class="title">
                <a class="" href="(.*?)"
                  title=".*?">(.*?)</a>
              </div>
'''
rst=re.compile(pat).findall(data,re.S)
print(rst)
image.png

意思就是转换为byte发送 这个网页只接收byte 不接收字符串
字符串前面加b就行了


image.png

然后


image.png
头部信息不能这么添加 要改为这种格式
User_Agent={'User-Agent':
'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
}
#第二个参数是data传入一些数据
req=urllib.request.Request("https://book.douban.com",None,headers=User_Agent)
data=urllib.request.urlopen(req).read().decode("utf-8")#改编码格式
pat=r'''
<div class="title">
                <a class="" href="(.*?)"
                  title=".*?">.*?</a>
              </div>
'''
print(data)
rst=re.compile(pat).findall(data,re.S)
print(rst)

网站是响应成功了
没有结果 print出来找规律
然后每次刷新新书速递都是不一样的
Ctrl+F 找规律 对比格式


image.png

如果查不到是因为print打印有上限 .decode("utf-8")转码去掉就可以查到

User_Agent={'User-Agent':
'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
}
#第二个参数是data传入一些数据
req=urllib.request.Request("https://book.douban.com",None,headers=User_Agent)
data=urllib.request.urlopen(req).read().decode("utf-8")#改编码格式
#找到其中一段
str= '''
<div class="title">\n                <a class="" href="https://book.douban.com/subject/34781358/?icn=index-latestbook-subject"\n                  title="\xe5\x8f\xb2\xe8\xae\xb0\xe7\x9a\x84\xe8\xaf\xbb\xe6\xb3\x95">\xe5\x8f\xb2\xe8\xae\xb0\xe7\x9a\x84\xe8\xaf\xbb\xe6\xb3\x95</a>\n
      </div>
'''

pat=r'<div class="title">\n\s*<a class="" href="(.*?)"\n\s*title="(.*?)">.*?</a>\n\s*</div>'

rst=re.compile(pat).findall(data,re.S)
print(rst)
image.png

然后我们写入到txt里
每一个正则表达式里()就会提取出来一个新的参数
这就是个列表


image.png
User_Agent={'User-Agent':
'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
}
#第二个参数是data传入一些数据
req=urllib.request.Request("https://book.douban.com",None,headers=User_Agent)
data=urllib.request.urlopen(req).read().decode("utf-8")#改编码格式
#找到其中一段
str= '''
<div class="title">\n                <a class="" href="https://book.douban.com/subject/34781358/?icn=index-latestbook-subject"\n                  title="\xe5\x8f\xb2\xe8\xae\xb0\xe7\x9a\x84\xe8\xaf\xbb\xe6\xb3\x95">\xe5\x8f\xb2\xe8\xae\xb0\xe7\x9a\x84\xe8\xaf\xbb\xe6\xb3\x95</a>\n
      </div>
'''

pat=r'<div class="title">\n\s*<a class="" href="(.*?)"\n\s*title="(.*?)">.*?</a>\n\s*</div>'

rst=re.compile(pat).findall(data,re.S)

#声明写入
fh=open(r"D:\py\简单爬虫\doubanNewBookSpeedLook.txt","w")
#开始写入文件
for i in range(0,len(rst)):
      fh.write("书名:"+rst[i][1]+" 连接:"+rst[i][0]+"\n")
fh.close()

然后去目录可以找到 可以自己改路径 和文件名


image.png

相关文章

  • PY爬虫极速学习 (九)简单爬虫编写

    然后只有vx二维码了 那就爬二维码吧我是360浏览器 基本都差不多 F12 然后这样点击可以直接跳到该图片的代码位...

  • PY爬虫极速学习 (七)了解爬虫

    定向采集网页某一块信息 比如B站某一类视频 某个人所有视频两种常用最基础的通用是不定向聚焦是有目的去采集 就是俗话...

  • 山东大学-VirtualJudge-总结1

    在这个周根据小组进度安排,我主要学习了Python爬虫的编写,学习主要参考:python实现简单爬虫功能 根据博客...

  • PY爬虫极速学习 (五)面向对象

    它是一个概念 之前学的就是面相过程 一路写下去面向对象核心 封装继承多态抽象就是为了复用 提高可读性 效率 输出 ...

  • PY爬虫极速学习 (三)函数 模块

    函数 封装就很熟悉了 就是面向对象三大特征 封装继承多态 两种变量 函数的定义与调用 模块 就类似于C#的 dll...

  • Scrapy资料

    1. Scrapy 介绍 经常发现使用python编写爬虫的相关文章,可以使用urllib2便可以简单的实现(Py...

  • 爬虫基础

    在编写一个爬虫之前,来学习下爬虫的基础知识: 爬虫:简单的可以理解为模拟浏览器向服务器发送HTTP请求,服务器接收...

  • Pycharm+Scrapy框架运行爬虫糗事百科(无items数

    scrapy爬虫框架 qsbk.py 爬虫代码 import scrapy'''scrapy框架爬虫流程:发送请求...

  • 3天玩转爬虫,爬取网页数据

    通过实战讲解如何编写爬虫,在实战中学习,最快的学习方法。自己总结的爬虫模板,网络数据任意爬,精华! 3天玩转爬虫,...

  • 各语言简单爬虫

    各语言简单爬虫 Python 简单爬虫 golang简单爬虫

网友评论

      本文标题:PY爬虫极速学习 (九)简单爬虫编写

      本文链接:https://www.haomeiwen.com/subject/scnawctx.html