首页 > 编程语言 >Python爬虫完整代码拿走就用

Python爬虫完整代码拿走就用

时间:2023-03-22 10:05:16浏览次数:38  
标签:item Python 爬虫 拿走 re html print data append

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

Python爬虫完整代码拿走就用_html

使用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=".\\douban.xls"
saveDate(datelist,savepath)

# askURL("http://jshk.com.cn")

findlink = re.compile(r'<a href="(.*?)">')
findimg = re.compile(r'<img.*src="(.*?)"',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>')

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”)

标签:item,Python,爬虫,拿走,re,html,print,data,append
From: https://blog.51cto.com/u_13488918/6142079

相关文章

  • Python爬虫完整代码拿走就用
    对于新手做Python爬虫来说是有点难处的,前期练习的时候可以直接套用模板,这样省时省力还很方便。使用Python爬取某网站的相关数据,并保存到同目录下Excel。直接上代码:imp......
  • python 编写一个程序用lambda查找输入给定的字符串是否是以'a'开头 是返回True
    https://www.cnblogs.com/frazer-cxy-jsfx/这段代码可以双重输入,双重判断,data1=input('请输入一个字符串:')#data2=input('请输入一个字符串:')aed=lambdax:......
  • 如何自己搭建Scrapy爬虫框架
    当你学了一段时间爬虫后,就会知道各种功能太多而且麻烦。还不如自己整理个框架方便的多。因此,从开始写爬虫程序开始,就会慢慢的接触到一些有关爬虫的框架、效率提升而且扩展......
  • Python对商店数据进行lstm和xgboost销售量时间序列建模预测分析|附代码数据
    全文下载链接:http://tecdat.cn/?p=17748最近我们被客户要求撰写关于销售量时间序列的研究报告,包括一些图形和统计输出在本文中,在数据科学学习之旅中,我经常处理日常工作中......
  • biopython Bio.Alphabet
    参考:https://biopython.org/wiki/Alphabet作用:1.记录序列的分子类型(DNA,RNA或蛋白质),2.在序列、alignment、motif等中声明预期的字符。在Biopython1.78及以后的版......
  • 便签_Python
    模糊合并中的笔记process.extractOne(query="中国平安",choices=["中国平安"],scorer=fuzz.token_set_ratio,score_cutoff=0)pr......
  • # yyds干货盘点 # 怎么用Python把左边这种转成右边这种?
    大家好,我是皮皮。一、前言前几天在Python白银交流群【惜君】问了一个​​Pandas​​处理Excel的问题,这里拿出来给大家分享下。下面是粉丝自己写的代码:二、实现过程看上去是......
  • Python 异步: 非阻塞流(20)
    动动发财的小手,点个赞吧!asyncio的一个主要好处是能够使用非阻塞流。1.异步流Asyncio提供非阻塞I/O套接字编程。这是通过流提供的。可以打开提供对流写入器和流写......
  • Python3中zipfile模块文件名乱码问题
    在zipfile.ZipFile中获得的filename有中日文则很大可能是乱码,这是因为在zip标准中,对文件名的encoding用的不是unicode,而可能是各种软件根据系统的默认字符集来采用(此......
  • Codon是一个高性能的Python编译器
    Codon是一个高性能的Python编译器,可以将Python代码编译成本地机器代码,没有任何运行时开销。与Python相比,单线程的典型加速比在10-100倍以上。Codon的性能通常与C/C++相......