python抓取头条美文并存储到mongodb

# Author:song
from multiprocessing import Pool
from urllib.parse import urlencode
import requests
import json
from requests import RequestException
from bs4 import BeautifulSoup
import re
import pymongo
client = pymongo.MongoClient('localhost',connect=False)
db = client['toutiaowenzhang'] def get_index(offset):
data = {
'offset': offset,
'format': 'json',
'keyword': '美文',
'autoload': 'true',
'count': 20,
'cur_tab': 1,
'from':'search_tab'
}
url = 'https://www.toutiao.com/search_content/?'+urlencode(data)
response = requests.get(url)
try:
if response.status_code == 200:
return response.text
else:
return None
except RequestException:
return None def get_urls(html):
data = json.loads(html)
if data and 'data' in data.keys():
for item in data.get('data'):
yield item.get('article_url') def get_index_detail(url):
response = requests.get(url)
try:
if response.status_code == 200:
return response.text
else:
return None
except RequestException:
return None def parse_detail(html):
try:
soup = BeautifulSoup(html,'lxml')
title = soup.select('title')[0].get_text()
compile_allarticle= re.compile('content.*?<div&gt(.*?)</div>',re.S)
allarticle = re.findall(compile_allarticle,html)
# article =re.sub('(<.*?<span>)','',allarticle[0])#正则匹配上不需要的那部分
article =re.sub('[a-zA-Z0-9/#;&\._]','',str(allarticle)).strip()#直接把字母数字全部替换
data = {
'title':title,
'article':article
}
return data
except TypeError:#解决出现了404界面
pass
def save_to_mongodb(result):
if db['toutiaowenzhang'].insert(result):
print('successful')
else:
print('fail') def main(offset):
html = get_index(offset)
items = get_urls(html)
for item in items:
if item:
ab = get_index_detail(item)
result = parse_detail(ab)
save_to_mongodb(result)
if __name__=='__main__':
groups = [x*20 for x in range(3)]
pool = Pool()
pool.map(main,groups)

python抓取头条文章的更多相关文章

  1. python抓取月光博客的全部文章而且依照标题分词存入mongodb中

    猛击这里:python抓取月光博客的全部文章

  2. 使用python抓取CSDN关注人的全部公布的文章

    # -*- coding: utf-8 -*- """ @author: jiangfuqiang """ import re import ...

  3. 使用 Python 抓取欧洲足球联赛数据

    Web Scraping在大数据时代,一切都要用数据来说话,大数据处理的过程一般需要经过以下的几个步骤    数据的采集和获取    数据的清洗,抽取,变形和装载    数据的分析,探索和预测    ...

  4. 用python抓取智联招聘信息并存入excel

    用python抓取智联招聘信息并存入excel tags:python 智联招聘导出excel 引言:前一阵子是人们俗称的金三银四,跳槽的小朋友很多,我觉得每个人都应该给自己做一下规划,根据自己的进步 ...

  5. 使用python抓取并分析数据—链家网(requests+BeautifulSoup)(转)

    本篇文章是使用python抓取数据的第一篇,使用requests+BeautifulSoup的方法对页面进行抓取和数据提取.通过使用requests库对链家网二手房列表页进行抓取,通过Beautifu ...

  6. Python抓取小说

    Python抓取小说 前言 这个脚本命令MAC在抓取小说写,使用Python它有几个码. 代码 # coding=utf-8 import re import urllib2 import chard ...

  7. Python 抓取网页并提取信息(程序详解)

    最近因项目需要用到python处理网页,因此学习相关知识.下面程序使用python抓取网页并提取信息,具体内容如下: #---------------------------------------- ...

  8. python抓取性感尤物美女图

    由于是只用标准库,装了python3运行本代码就能下载到多多的美女图... 写出代码前面部分的时候,我意识到自己的函数设计错了,强忍继续把代码写完. 测试发现速度一般,200K左右的下载速度,也没有很 ...

  9. python抓取网页例子

    python抓取网页例子 最近在学习python,刚刚完成了一个网页抓取的例子,通过python抓取全世界所有的学校以及学院的数据,并存为xml文件.数据源是人人网. 因为刚学习python,写的代码 ...

随机推荐

  1. Java实现 洛谷 P1328 生活大爆炸版石头剪刀布

    import java.util.Scanner; public class Main{ private static int[] duel(int playerA, int playerB){ in ...

  2. shell命令:命令置换、进程管理

    1:命令置换 command1  `command2` 将command2的结果作为command1的参数 注意:command2的引号为esc键下的单引号 2:进程管理 1)命令 (1)ps  -a ...

  3. Windows下虚拟机Linux(CentOS8)扩容设置 - 磁盘扩容中的坑和解决方法

    摘要:[原创]转载请注明作者Johnthegreat和本文链接 由于虚拟机空间不足,为了避免重装虚拟机,做了一次无损扩容.   过程中的报错如下: [root@localhost ~]# pvcrea ...

  4. 重学 Java 设计模式:实战组合模式(营销差异化人群发券,决策树引擎搭建场景)

    作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 小朋友才做选择题,成年人我都要 头几年只要群里一问我该学哪个开发语言,哪个语言最好. ...

  5. springmvc无法进入controller,且报错404

    今天搭建一个springmvc项目时,前台一直报错404,在controller中调试发现程序没有进入controller. 通过多次刷新前台页面,发现第一次进入是会弹出错误提示,第二次之后就直接40 ...

  6. Node第三方模块

    node第三方模块集合 1.nrm 切换npm下载的镜像地址 nrm ls   查看可用镜像 nrm use +镜像名 2.nodemon 在控制台nodenom替代node命令执行nodejs文件, ...

  7. xlwings--Python for Excel

    xlwings 中文文档 xlwings,让excel飞起来! xlwings 的使用教程

  8. 2.使用nexus3配置docker私有仓库

    1,配置走起 1,创建blob存储 登陆之后,先创建一个用于存储镜像的空间. 定义一个name,下边的内容会自动补全. 然后保存. 注意:实际生产中使用,建议服务器存储500G或以上. 2,创建一个h ...

  9. [转] VMware中的Ubuntu无法通过桥接方式上网

    遇到的问题:主机可以上网(使用了代理),VMware设置为桥接方式连网.在主机中可以ping通虚拟机,在虚拟机中也可以ping通主机,可是在虚拟机中始终连接不了因特网. 感谢刘洋同学的博文,“在桥接模 ...

  10. easypoi 读取 Excel 简单应用

    背景 在做接口测试的时候,经常会使用 Excel 来存储对应的接口信息和用例信息,为了方便程序的读取,引入easypoi 工具来读取 Excel 内容.easypoi 比起 poi 使用更加的方便,代 ...