crawl blog website: www.apress.com

# -*- coding: utf-8 -*-
"""
Created on Wed May 10 18:01:41 2017
@author: Raghav Bali
""" """
This script crawls apress.com's blog page to:
+ extract list of recent blog post titles and their URLS
+ extract content related to each blog post in plain text
using requests and BeautifulSoup packages
``Execute``
$ python crawl_bs.py
""" import requests
from time import sleep
from bs4 import BeautifulSoup def get_post_mapping(content):
"""This function extracts blog post title and url from response object
Args:
content (request.content): String content returned from requests.get
Returns:
list: a list of dictionaries with keys title and url
"""
post_detail_list = []
post_soup = BeautifulSoup(content,"lxml")
h3_content = post_soup.find_all("h3") for h3 in h3_content:
post_detail_list.append(
{'title':h3.a.get_text(),'url':h3.a.attrs.get('href')}
) return post_detail_list def get_post_content(content):
"""This function extracts blog post content from response object
Args:
content (request.content): String content returned from requests.get
Returns:
str: blog's content in plain text
"""
plain_text = ""
text_soup = BeautifulSoup(content,"lxml")
para_list = text_soup.find_all("div",
{'class':'cms-richtext'}) for p in para_list[0]:
plain_text += p.getText() return plain_text if __name__ =='__main__': crawl_url = "http://www.apress.com/in/blog/all-blog-posts"
post_url_prefix = "http://www.apress.com" print("Crawling Apress.com for recent blog posts...\n\n") response = requests.get(crawl_url) if response.status_code == 200:
blog_post_details = get_post_mapping(response.content) if blog_post_details:
print("Blog posts found:{}".format(len(blog_post_details))) for post in blog_post_details:
print("Crawling content for post titled:",post.get('title'))
post_response = requests.get(post_url_prefix+post.get('url')) if post_response.status_code == 200:
post['content'] = get_post_content(post_response.content) print("Waiting for 10 secs before crawling next post...\n\n")
sleep(10) print("Content crawled for all posts") # print/write content to file
for post in blog_post_details:
print(post)

python crawler的更多相关文章

  1. Python crawler access to web pages the get requests a cookie

    Python in the process of accessing the web page,encounter with cookie,so we need to get it. cookie i ...

  2. 【python爬虫】根据查询词爬取网站返回结果

    最近在做语义方面的问题,需要反义词.就在网上找反义词大全之类的,但是大多不全,没有我想要的.然后就找相关的网站,发现了http://fanyici.xpcha.com/5f7x868lizu.html ...

  3. python脚本工具 - 3 目录遍历

    遍历系统中某一目录下的所有文件名 #! /usr/bin/python # coding:utf-8 import os def dirList(path): filelist = os.listdi ...

  4. pyrailgun 0.24 : Python Package Index

    pyrailgun 0.24 : Python Package Index pyrailgun 0.24 Download pyrailgun-0.24.zip Fast Crawler For Py ...

  5. [Python]新手写爬虫全过程(转)

    今天早上起来,第一件事情就是理一理今天该做的事情,瞬间get到任务,写一个只用python字符串内建函数的爬虫,定义为v1.0,开发中的版本号定义为v0.x.数据存放?这个是一个练手的玩具,就写在tx ...

  6. python编写知乎爬虫实践

    爬虫的基本流程 网络爬虫的基本工作流程如下: 首先选取一部分精心挑选的种子URL 将种子URL加入任务队列 从待抓取URL队列中取出待抓取的URL,解析DNS,并且得到主机的ip,并将URL对应的网页 ...

  7. python爬虫之urllib

    #coding=utf-8 #urllib操作类 import time import urllib.request import urllib.parse from urllib.error imp ...

  8. Python实现自动登录/登出校园网网关

    学校校园网的网络连接有免费连接和收费连接两种类型,可想而知收费连接浏览体验更佳,比如可以访问更多的网站.之前收费地址只能开通包月服务才可使用,后来居然有了每个月60小时的免费使用收费地址的优惠.但是, ...

  9. python爬虫实践

    模拟登陆与文件下载 爬取http://moodle.tipdm.com上面的视频并下载 模拟登陆 由于泰迪杯网站问题,测试之后发现无法用正常的账号密码登陆,这里会使用访客账号登陆. 我们先打开泰迪杯的 ...

随机推荐

  1. [转]解决ubuntu16.04 ‘E: 无法获得锁 /var/lib/dpkg/lock-frontend - open (11: 资源暂时不可用) ’ 问题

    当运行sudo apt-get install/update/其他命令时,会出现如下提示: E: 无法获得锁 /var/lib/dpkg/lock-frontend - open (11: 资源暂时不 ...

  2. Apache配置WebSocket代理

    引入mod_proxy_wstunnel.so模块,使其可以解析ws.wss协议 LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel ...

  3. js中 json对象与json字符串相互转换的几种方式

    以下总结js中 json对象与json字符串相互转换的几种方式: 一.JSON对象转化为JSON字符串 1.使用JSON.stringify()方法进行转换 该方法不支持较老版本的IE浏览器,比如:i ...

  4. echarts曲线 将不连续的点连接起来、由null间断的点连接起来

    series:[ { name: '测试',  type: 'line',  data: [1,null,2,null,null,3,null,null,null,4],  connectNulls: ...

  5. LP线性规划初识

    认识LP 线性规划(Linear Programming) 特指目标函数和约束条件皆为线性的最优化问题. 目标函数: 多个变量形成的函数 约束条件: 由多个等式/不等式形成的约束条件 线性规划: 在线 ...

  6. Mysql 控制结构初识

    Mysql 流程控制 认识 从我目前所接触的编程语言,C, R, VB, Python, Javascript...,来看, 无非就是变量, 表达式, 流程控制(顺序, 分支, 循环), 封装了一些更 ...

  7. 为什么要将action实例设置为多例

    转载自 https://zhidao.baidu.com/question/622162406833405932.html struts2中action是多例的,即一个session产生一个actio ...

  8. TI DSP数据长度

    环境CCS7.2 平台C6748 结果: sizeof short int is 2sizeof int is 4sizeof long is 4sizeof unsigned long is 4si ...

  9. C++学习(7)—— 函数提高

    1. 函数默认参数 在C++中,函数的形参列表中的形参是可以有默认值的 语法:返回值类型 函数名 (参数=默认值){} 注意 如果某个位置已经有了默认参数,那么从这个位置往后,从左到右都必须有默认值 ...

  10. 项目Beta冲刺(团队)--5/7

    课程名称:软件工程1916|W(福州大学) 作业要求:项目Beta冲刺 团队名称:葫芦娃队 作业目标:进行新一轮的项目冲刺,尽力完成并完善项目 团队博客 队员学号 队员昵称 博客地址 04160242 ...