当前位置:网站首页>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'')#The number of reviewersfindJudge = 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() 边栏推荐
猜你喜欢

2022杭电多校4

字节API鉴权方法

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

全球电子产品需求放缓,三星手机越南工厂每周只需要干 3~4 天

How to monitor code cyclomatic complexity by refactoring indicators

NUS颜水成等发布首篇《深度长尾学习》综述

弄懂#if #ifdef #if defined

2022 Hangzhou Electric Multi-School 4

To ensure that the communication mechanism

IP第十八天笔记
随机推荐
tif转mat
使用百度EasyDL实现森林火灾预警识别
RSA306B,500,600系列API接口代码
Request method ‘POST‘ not supported。 Failed to load resource: net::ERR_FAILED
初学爬虫笔记(收集数据)
QT笔记——Q_INVOKABLE了解
不需要服务器,教你仅用30行代码搞定实时健康码识别
Next -18- 添加代码复制按钮
基于 Next.js实现在线Excel
Go 言 Go 语,一文看懂 Go 语言文件操作
李沐的深度学习笔记来了!
Li Mu's deep learning notes are here!
RepVGG学习笔记
Resharper 如何把类里的类移动到其他文件
Next -20- 使用自定义样式 (custom style)
What are the useful IT asset management platforms?
Latex 去掉行号
The electromagnetic compatibility EMC protection study notes
Next -19- 开启fancybox查看图片大图
DocuWare Platform - Content Services and Workflow Automation Platform for Document Management (Part 1)