Python爬虫完整代码拿走不谢
创始人
2024-12-27 15:40:32
0

对于新手做Python爬虫来说是有点难处的,前期练习的时候可以直接套用模板,这样省时省力还很方便。

使用Python爬取某网站的相关数据,并保存到同目录下Excel。

直接上代码:

import re import urllib.error import urllib.request  import xlwt from bs4 import BeautifulSoup   def main():     baseurl ="http://jshk.com.cn"      datelist = getDate(baseurl)     savepath=".\\jshk.xls"     saveDate(datelist,savepath)      # askURL("http://jshk.com.cn/")  findlink = re.compile(r'') findimg = re.compile(r'(.*)(.*)(\d*)人评价') findinq= re.compile(r'(.*)')  def getDate(baseurl):     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)[0]              rating=re.findall(findrating,item)[0]             data.append(rating)             judge=re.findall(findjudge,item)[0]             data.append(judge)             inq=re.findall(findinq,item)              if len(inq)!=0:                 inq=inq[0].replace("。","")                 data.append(inq)             else:                 data.append(" ")             print(data)             datalist.append(data)         print(datalist)     return datalist  def askURL(url):     head = {     "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36"}     request=urllib.request.Request(url,headers=head)     html=""     try:         response=urllib.request.urlopen(request)         html=response.read().decode("utf-8")         # print(html)     except urllib.error.URLError as e:         if hasattr(e,"code"):             print(e.code)         if hasattr(e,"reason"):             print(e.reason)      return html  def saveDate(datalist,savepath):     workbook = xlwt.Workbook(encoding='utf-8')     worksheet = workbook.add_sheet('电影',cell_overwrite_ok=True)     col =("电影详情","图片","影片","评分","评价数","概况")     for i in range(0,5):         worksheet.write(0,i,col[i])     for i in range(0,250):         print("第%d条" %(i+1))         data=datalist[i]         for j in range(0,5):             worksheet.write(i+1,j,data[j])      workbook.save(savepath)    if __name__ == '__main__':     main()     print("爬取完毕") 

直接复制粘贴就行。

若要更改爬取网站,则需要更改URL以及相应的html格式(代码中的“item”)。

相关内容

热门资讯

透视机巧!wpk透视辅助(透视... 透视机巧!wpk透视辅助(透视)开挂脚本工具(哔哩哔哩)1、让任何用户在无需wpk透视辅助安装教程第...
透视步骤!hhpoker脚本(... 透视步骤!hhpoker脚本(透视)开挂脚本工具(哔哩哔哩)1、hhpoker脚本模拟器是什么优化,...
透视模块!德州透视hhpoke... 您好,德州透视hhpoker这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】...
透视方针!德州机器人代打脚本(... 透视方针!德州机器人代打脚本(透视)开挂透视下载(哔哩哔哩)1、首先打开德州机器人代打脚本辅助器下载...
透视窍要!hhpoker万能辅... 透视窍要!hhpoker万能辅助器(透视)开挂透视修改器(哔哩哔哩)1、不需要AI权限,帮助你快速的...
透视积累!wepoker透视器... 透视积累!wepoker透视器免费(透视)开挂脚本下载(哔哩哔哩)1、实时wepoker透视器免费透...
透视阶段!hhpoker辅助挂... 透视阶段!hhpoker辅助挂是真的吗(透视)开挂脚本插件(哔哩哔哩)hhpoker辅助挂是真的吗辅...
透视手筋!hhpoker辅助(... 透视手筋!hhpoker辅助(透视)开挂脚本神器(哔哩哔哩)1、每一步都需要思考,不同水平的挑战会更...
透视资料!hhpoker德州透... 透视资料!hhpoker德州透视(透视)开挂脚本平台(哔哩哔哩)1、每一步都需要思考,不同水平的挑战...
透视办法!约局吧辅助器(透视)... 透视办法!约局吧辅助器(透视)开挂脚本平台(哔哩哔哩)该软件可以轻松地帮助玩家将约局吧辅助器外卦神器...