当前位置:网站首页>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 Hangzhou Electric Multi-School 4
Redis 高可用
Semaphore 基本原理
Redis-主从复制
如何优雅的消除系统重复代码?
remote: Check Access Error, please check your access right or username and password!fatal: Authenti
直播回放含 PPT 下载|基于 Flink & DeepRec 构建 Online Deep Learning
一文解答DevOps平台的制品库是什么
What is the difference between ITSM software and a work order system?
洛谷题解P4326 求圆的面积
随机推荐
Online Excel based on Next.js
爬虫小白笔记(昨天的对于注意解析数据的补充)
【已解决】allure无法生成json文件和AttributeError: module ‘allure‘ has no attribute ‘severity_level‘
C端折戟,转战B端,联想的元宇宙梦能成吗?
QT笔记——QUuid了解
实战:10 种实现延迟任务的方法,附代码!
IP第十六天笔记
GPS satellite synchronization clock, NTP network synchronization clock, Beidou clock server (Jingzhun)
2022 Hangzhou Electric Multi-School 4
PHP 图片转PDF
##ansible自动化运维架构与简介
DevOps平台中的制品库是什么?有什么用处?
MVCC实现过程
2022年7月国产数据库大事记-墨天轮
inter-process communication
洛谷题解P1028 数的计算
uni-app之renderjs
性能提升400倍丨外汇掉期估值计算优化案例
ITSM软件与工单系统的区别是什么?
邮差"头":{“retCode”:“999999”