python爬取当前(CSDN)文章更新

在这里插入图片描述

#encoding:UTF-8
#测试爬虫例子
#爬取网页内容,保存数据库,
#下载图片,上传七牛云
#!/usr/bin/python3
# encoding:utf-8
'''
Created on 2020-11-11 08:20
@author: king
res = requests.get(url,params=data,headers = headers)
res = requests.post(url, data=json_data, headers = headers)
'''
import requests
import sys,os
import time
from bs4 import BeautifulSoup
from login import models
from login import qintupin
import hashlib

class csdn(object):
	"""docstring for ClassName"""
	def __init__(self, href):
		super(csdn, self).__init__()
		self.href = href
		
    #获取页面信息
	def gethtml(self,helf):
	 	headers = {
	 	'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
	 	}
	 	response = requests.get(helf,data={},headers=headers)
	 	return response.text
    #下载页面图片
	def downimgs(self):
	 	kpath = os.path.join("D:/python/k5003/login/", "/images/")
	 	strimg=self.href.split("/")[-1]
	 	headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"}
	 	html = requests.get(self.href,data={},headers=headers)
	 	print(kpath+strimg)
	 	filepath="D:/python/k5003/login/images/"+strimg
		# 将图片保存到D盘,然后上传七牛云
	 	with open(filepath,"wb") as f:
	 		f.write(html.content)
	 		time.sleep(2)
	 		tupian=qintupin.uploadqiniu("1222252","","")
	 		token2=tupian.upload_token()
	 		tupian.token=token2
	 		tupian.filepath=filepath
	 		tupian.filename=strimg
	 		tupian.update_file()


    #获取指定信息
	def get_content(self):
	 	href = "https://blog.csdn.net/qqjjj"
	 	html = self.gethtml(href)
	 	#print(type(response.text),type(response.content))
	 	soup = BeautifulSoup(html,"html.parser")
	 	lables = soup.find('div', class_="article-list").find_all(class_="article-item-box")
	 	about = soup.find('div', class_="article-list").find_all("p",class_="content")
	 	data=[];
	 	i=0;
	 	a = hashlib.md5()
	 	for x in lables:
	 		print(x);
	 		href=dict(x.find("a").attrs)["href"]
	 		aboutstr=x.find("p",class_="content").text
	 		print(aboutstr)
	 		self.href=href
	 		html2=self.gethtml(href)
	 		soup2 = BeautifulSoup(html2,"html.parser")
	 		title=soup2.find('h1', id="articleContentId")
	 		datatime=soup2.find('span', class_="time")
	 		content=soup2.find('div', id="article_content")
	 		clickno=soup2.find('span', class_="read-count")
	 		#获取图片信息
	 		img=content.find_all("img")
	 		markstr=str(content).replace("https://img-blog.csdnimg.cn","https://img.k5003.com")
	 		time.sleep(2)
	 		imgurl="";
	 		for k in img:
	 			imgurl=dict(k.attrs)["src"].split("?")[0]
	 			self.href=imgurl
	 			self.downimgs()
	 			print(imgurl)
	 		#data.append(title)
	 		#判断是否存在
	 		a.update(href.encode(encoding='utf-8'))
	 		kmd5=a.hexdigest()
	 		models.fcomplex.objects.filter(f_md5url=kmd5)
	 		#保存数据到表
	 		i=i+1
	 		fcomplex = models.fcomplex(
                f_id=i,
                f_md5url=kmd5,
                f_name=str(title.text),
                f_createname="king",
                f_clickno=clickno.text,
                f_addtime=datatime.text,
                f_key=str(title.text),
                f_about=str(aboutstr),
                f_maxurl=str(imgurl).replace("https://img-blog.csdnimg.cn","https://img.k5003.com"),
                f_remark=markstr
                )
	 		fcomplex.save()
	 	return data

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

k5003

您的鼓励将是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值