爬取王垠的博客并生成pdf
尚未完善,有待改进
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = 'jiangwenwen'
import pdfkit
import time
import requests
import random
from bs4 import BeautifulSoup
from fake_useragent import UserAgent
# 请求头
ua = UserAgent()
headers = {
'cache-control': "no-cache",
"Host": "www.yinwang.org",
"User-Agent": ua.random,
"Referer": "http://www.yinwang.org/",
}
# IP代理池
ip_pool = ['123.55.114.217:9999',
'110.52.235.91:9999',
'183.163.43.61:9999',
'119.101.126.52:9999',
'119.101.124.165:9999',
'119.101.125.38:9999',
'119.101.125.84:9999',
'110.52.235.80:9999',
'119.101.125.49:9999',
'110.52.235.162:9999',
'119.101.124.23:9999'
]
# 打印成pdf
def print_pdf(url, file_name):
start = time.time()
print("正在打印中...")
headers["User-Agent"] = ua.random
print("User-Agent是:{0}".format(headers["User-Agent"]))
content = requests.get(url, headers=headers, timeout=3, proxies=get_proxy(ip_pool)).text
pdfkit.from_string(content, file_name)
end = time.time()
print("打印成功,本次打印耗时:%0.2f秒" % (end - start))
# 获得有效代理
def get_proxy(ip_pool):
for ip in ip_pool:
url = "http://www.yinwang.org/"
# 用requests来验证ip是否可用
try:
requests.get(url, proxies={"http": "http://{}".format(ip), }, timeout=3)
except:
continue
else:
proxies = {
"http": "http://{}".format(ip),
"https": "http://{}".format(ip),
}
return proxies
response = requests.get("http://www.yinwang.org/", headers=headers, proxies=get_proxy(ip_pool))
soup = BeautifulSoup(response.content, 'html.parser')
tags = soup.find_all("li", class_="list-group-item title")
for child in tags:
article_url = "http://www.yinwang.org" + child.a.get('href')
article_file_name = "桌面\\" + child.a.string + ".pdf"
print_pdf(article_url, article_file_name)
爬取王垠的博客并生成pdf的更多相关文章
- 一文搞定scrapy爬取众多知名技术博客文章保存到本地数据库,包含:cnblog、csdn、51cto、itpub、jobbole、oschina等
本文旨在通过爬取一系列博客网站技术文章的实践,介绍一下scrapy这个python语言中强大的整站爬虫框架的使用.各位童鞋可不要用来干坏事哦,这些技术博客平台也是为了让我们大家更方便的交流.学习.提高 ...
- Python 2.7_爬取CSDN单页面博客文章及url(二)_xpath提取_20170118
上次用的是正则匹配文章title 和文章url,因为最近在看Scrapy框架爬虫 需要了解xpath语法 学习了下拿这个例子练手 1.爬取的单页面还是这个rooturl:http://blog.csd ...
- 我是如何将博客转成PDF的
前言 只有光头才能变强 之前有读者问过我:"3y你的博客有没有电子版的呀?我想要份电子版的".我说:"没有啊,我没有弄过电子版的,我这边有个文章导航页面,你可以去文章导航 ...
- 使用Windows Live Writer拉取之前写的博客
因为之前写的博客有错误需要修改,但是在Windows Live Writer中找了半天也没找到怎么拉取之前的博客,在[打开本地草稿]或者[打开最近使用过的日志]中,由于存储的项数有限,所以就找不到那篇 ...
- 使用Python爬取微信公众号文章并保存为PDF文件(解决图片不显示的问题)
前言 第一次写博客,主要内容是爬取微信公众号的文章,将文章以PDF格式保存在本地. 爬取微信公众号文章(使用wechatsogou) 1.安装 pip install wechatsogou --up ...
- 将博客转成pdf
前些天无意间看到了“birdben”的博客,写的比较详细,但是最新的文章更新时间是“2017-05-07”,时间很是久远,本打算有时间认真学习一下博主所写的文章,但是担心网站会因为某些原因停止服务,于 ...
- Java爬取B站弹幕 —— Python云图Wordcloud生成弹幕词云
一 . Java爬取B站弹幕 弹幕的存储位置 如何通过B站视频AV号找到弹幕对应的xml文件号 首先爬取视频网页,将对应视频网页源码获得 就可以找到该视频的av号aid=8678034 还有弹幕序号, ...
- flask实战-个人博客-数据库-生成虚拟数据 --
3.生成虚拟数据 为了方便编写程序前台和后台功能,我们在创建数据库模型后就编写生成虚拟数据的函数. 1)管理员 用于生成虚拟管理员信息的fake_admin()函数如下所示: personalBlog ...
- 爬取拉勾网python工程师的岗位信息并生成csv文件
转载自:https://www.cnblogs.com/sui776265233/p/11146969.html 代码写得很好,但是目前只看得懂前一部分 一.爬取和分析相关依赖包 Python版本: ...
随机推荐
- Vue 实现一个分页组件
实现分页组件要分三个部分 样式,逻辑,和引用 首先新建一个vue文件用来承载组件内容 第一步:构建样式 <template> <nav> <ul class=" ...
- Azkaban 2.5.0的详细安装过程
准备下载Azkaban2.5.0:https://azkaban.github.io/downloads.htm 准备插件: 一.MySQL安装与配置 启动数据库并查看状态:sudo service ...
- openstack stein部署手册 3. keystone
# 建立数据库用户及权限 create database keystone; grant all privileges on keystone.* to keystone@'localhost' id ...
- oralce 汇编02
Assembler Directives .align integer, padThe .align directive causes the next data generated to be al ...
- Reference与ReferenceQueue
Reference源码分析 首先我们先看一下Reference类的注释: /** * Abstract base class for reference objects. This class def ...
- sysbench github & manual
sysbench github https://github.com/akopytov/sysbench sysbench-manual.pdf https://github.com/mrivandu ...
- JSON.stringify常见用法
转摘于其他博客 var data =[ { name: "金",sex:"1",age:26 }, { name: "才",sex:&quo ...
- 线程数设置和CPU数的关系
一般说来,大家认为线程池的大小经验值应该这样设置:(其中N为CPU的个数) 如果是CPU密集型应用,则线程池大小设置为N+1 如果是IO密集型应用,则线程池大小设置为2N+1(因为io读数据或者缓存的 ...
- 37行代码构建无状态组件通信工具-让恼人的Vuex和Redux滚蛋吧!
状态管理的现状 很多前端开发者认为,Vuex和Redux是用来解决组件间状态通信问题的,所以大部分人仅仅是用于达到状态共享的目的.但是通常Redux是用于解决工程性问题的,用于分离业务与视图,让结构更 ...
- python tkinter画圆
x0=150 #圆心横坐标 y0=100 #圆心纵坐标 canvas.create_oval(x0-10,y0-10,x0+10,y0+10) #圆外矩形左上角与右下角坐标 canv ...