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



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


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

然后粘贴过去 ” 会格式不对改为' 或者字符串改为'
#自动提取课程页面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)

然后再试一个爬豆瓣的

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

在这里

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)

恭喜触发彩蛋

然后根据网上说要加个head避过爬虫
打开要爬的网页F12 F5 根据箭头依次找到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=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)

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

然后

头部信息不能这么添加 要改为这种格式
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 找规律 对比格式

如果查不到是因为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)

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

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()
然后去目录可以找到 可以自己改路径 和文件名

网友评论