当前位置:网站首页>爬虫小白笔记(昨天的对于注意解析数据的补充)
爬虫小白笔记(昨天的对于注意解析数据的补充)
2022-08-04 15:25:00 【总是汗水大过天赋】
import re
import urllib.request
from bs4 import BeautifulSoup
def main():
#1.抓取网页(在这里面逐一解析数据)
baseurl = 'https://movie.douban.com/top250?start='
datalist = getData(baseurl)
#2.保存数据
print()
#电影链接
findLink = re.compile(r'<a href="(.*?)">')
#电影图片
findImg = re.compile(r'<img.*src="(.*?)"',re.S)#re.S表示匹配成功的字符里面可以忽略空格(当你想要的内容里面有许多空格,你想要它里面出来没有空格时使用)
#影片片名
findTitle = re.compile(r'<span class="title">(.*?)</span>')
#影片评分
findRating = re.compile(r'<span class="rating_num" property="v:average">(.*?)</span>')
#评价人数
findJudge = re.compile(r'<span>(\d*)人评价</span>')
#概况
findInq = re.compile(r'<span class="inq">(.*?)</span>')
#影片相关内容
findBd = re.compile(r'<p class="">(.*?)</p>',re.S)
#抓取网页
def getData(baseurl):
#首先需要获取一页的数据,然后利用循环来获取每一页的信息
#datalist每一个存储一页数据
datalist = []
for i in range(0,10):
url = baseurl + str(i*25)
html = askURL(url)
#在循环里逐一解析每一页的数据
#循环解析每一个电影
soup = BeautifulSoup(html,"html.parser")
for item in soup.find_all('div',class_ = 'item'):
data = []#用来存储每一个电影的信息
item = str(item)
link = re.findall(findLink,item)[0]
data.append(link)
img = re.findall(findImg,item)[0]
data.append(img)
title = re.findall(findTitle,item)
if(len(title) == 2) :
ctitle = title[0]
data.append(ctitle)
etitle = title[1].replace("/","")
data.append(etitle)
else:
data.append(title[0])
data.append(' ')#当不存在英文名字时,用空格把位置留着
judgeNum = re.findall(findJudge,item)[0]
data.append(judgeNum)
Inq = re.findall(findInq,item)
if len(Inq) != 0:
inq = Inq[0].replace('。'," ")
data.append(inq)
else :
data.append(" ")#如果没有则留空
bd = re.findall(findBd,item)[0]
bd = re.sub('<br(\s+)?>(\s+)?'," ",bd)
bd = re.sub("/"," ",bd)
data.append(bd.strip())#去掉前后的空格
datalist.append(data)#把处理好的一部电影信息放入datalist里面
print(datalist)
return datalist
#请求网页
def askURL(url):
header = {
"User-Agent":"Mozilla/5.0(Linux;Android6.0;Nexus5 Build / MRA58N) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 103.0.5060.134MobileSafari / 537.36Edg / 103.0.1264.77"
}
request = urllib.request.Request(url,headers = header)
html = ""
try :
responce = urllib.request.urlopen(request)
html = responce.read().decode()
except urllib.error.URLerror as e:
if hasattr(e,"code"):
print(e.code)
if hasattr(e,"reason"):
print(e.reason)
return html
#保存数据
def saveData() :
print()
if __name__ == '__main__':
main()边栏推荐
猜你喜欢

IP第十七天笔记

Compound Refractive Lenses for X-ray Focusing

如何优雅的消除系统重复代码?

Semaphore 基本原理

Hangzhou Electric School Competition (Counter Attack Index)

Redis 高可用

C端折戟,转战B端,联想的元宇宙梦能成吗?

Legal education combined with VR panorama, intuitively feel and learn the spirit of the rule of law

界面组件DevExpress ASP.NET Core v22.1 - 增强数据导出功能

推荐一个鸿蒙即时通讯软件《果聊》
随机推荐
指数族分布与最大熵
numpy入门详细代码
leetcode: 254. Combinations of factors
Taurus.MVC WebAPI 入门开发教程2:添加控制器输出Hello World。
C端折戟,转战B端,联想的元宇宙梦能成吗?
Oracle database user creation, restart, import and export
AIX7.1安装Oracle11g补丁33829709(PSU+OJVM)
Leetcode: 215 disorderly to find the first big k element in the array
2022杭电多校3
How to fall in love with a programmer
推荐一个鸿蒙即时通讯软件《果聊》
Cisco - Small Network Topology (DNS, DHCP, Web Server, Wireless Router)
Go 言 Go 语,一文看懂 Go 语言文件操作
Semaphore 基本原理
如何优雅的消除系统重复代码?
小程序|炎炎夏日、清爽一夏、头像大换装
宣传海报
微软表示将向内部网络安全专家共享数据 为企业提供更安全保护
从-99打造Sentinel高可用集群限流中间件
leetcode: 255 Verify preorder traversal sequence binary search tree