当前位置:网站首页>Crawler Xiaobai Notes (yesterday's supplement to pay attention to parsing data)
Crawler Xiaobai Notes (yesterday's supplement to pay attention to parsing data)
2022-08-04 15:40:00 【Always sweat than talent】
import reimport urllib.requestfrom bs4 import BeautifulSoupdef main():#1. Crawl the web (parse the data one by one in this)baseurl = 'https://movie.douban.com/top250?start='datalist = getData(baseurl)#2. Save dataprint()#movie linkfindLink = re.compile(r'')#movie picturesfindImg = re.compile(r'(.*?)')#video ratingfindRating = re.compile(r'(\d*)people evaluation')#profilefindInq = re.compile(r'(.*?)')#video related contentfindBd = re.compile(r'(.*?)
',re.S)#crawl the webdef getData(baseurl):#First you need to get a page of data, and then use a loop to get the information of each page#datalist stores one page of data eachdatalist = []for i in range(0,10):url = baseurl + str(i*25)html = askURL(url)#Parse the data of each page one by one in the loop#loop through each moviesoup = BeautifulSoup(html,"html.parser")for item in soup.find_all('div',class_ = 'item'):data = []# used to store the information of each movieitem = 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(' ')#When there is no English name, keep the position with a spacejudgeNum = 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(" ")#If there is none, leave it blankbd = re.findall(findBd,item)[0]bd = re.sub('
(\s+)?'," ",bd)bd = re.sub("/"," ",bd)data.append(bd.strip())#Remove the spaces before and afterdatalist.append(data)# Put the processed movie information into the datalistprint(datalist)return datalist#Request web pagedef 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 :response = urllib.request.urlopen(request)html = response.read().decode()except urllib.error.URLerror as e:if hasattr(e,"code"):print(e.code)if hasattr(e,"reason"):print(e.reason)return html#save datadef saveData() :print()if __name__ == '__main__':main() ')#The number of reviewersfindJudge = re.compile(r'
边栏推荐
猜你喜欢
随机推荐
《2022 年上半年全球独角兽企业发展研究报告》发布——DEMO WORLD世界创新峰会圆满落幕
Redis-哨兵模式
洛谷题解P4326 求圆的面积
界面组件DevExpress ASP.NET Core v22.1 - 增强数据导出功能
Next -21- 添加相册系列 - 1- 框架设置
什么是 DevOps?看这一篇就够了!
李沐的深度学习笔记来了!
全球电子产品需求放缓,三星手机越南工厂每周只需要干 3~4 天
阿尔萨斯监控平台&普罗米修斯监控平台对服务器资源的监控
基于 Next.js实现在线Excel
Go 事,Gopher 要学的数字类型,变量,常量,运算符 ,第2篇
多线程编程之优先级翻转问题
【Go事】一眼看穿 Go 的集合和切片
从-99打造Sentinel高可用集群限流中间件
爬虫小白笔记(昨天的对于注意解析数据的补充)
PTA 6-2 多项式求值
【云原生 | 从零开始学Kubernetes】kubernetes之StatefulSet详解
不需要服务器,教你仅用30行代码搞定实时健康码识别
Flutter 运动鞋商铺小demo
多商户商城系统功能拆解24讲-平台端分销会员