python crawler
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的更多相关文章
- 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 ...
- 【python爬虫】根据查询词爬取网站返回结果
最近在做语义方面的问题,需要反义词.就在网上找反义词大全之类的,但是大多不全,没有我想要的.然后就找相关的网站,发现了http://fanyici.xpcha.com/5f7x868lizu.html ...
- python脚本工具 - 3 目录遍历
遍历系统中某一目录下的所有文件名 #! /usr/bin/python # coding:utf-8 import os def dirList(path): filelist = os.listdi ...
- pyrailgun 0.24 : Python Package Index
pyrailgun 0.24 : Python Package Index pyrailgun 0.24 Download pyrailgun-0.24.zip Fast Crawler For Py ...
- [Python]新手写爬虫全过程(转)
今天早上起来,第一件事情就是理一理今天该做的事情,瞬间get到任务,写一个只用python字符串内建函数的爬虫,定义为v1.0,开发中的版本号定义为v0.x.数据存放?这个是一个练手的玩具,就写在tx ...
- python编写知乎爬虫实践
爬虫的基本流程 网络爬虫的基本工作流程如下: 首先选取一部分精心挑选的种子URL 将种子URL加入任务队列 从待抓取URL队列中取出待抓取的URL,解析DNS,并且得到主机的ip,并将URL对应的网页 ...
- python爬虫之urllib
#coding=utf-8 #urllib操作类 import time import urllib.request import urllib.parse from urllib.error imp ...
- Python实现自动登录/登出校园网网关
学校校园网的网络连接有免费连接和收费连接两种类型,可想而知收费连接浏览体验更佳,比如可以访问更多的网站.之前收费地址只能开通包月服务才可使用,后来居然有了每个月60小时的免费使用收费地址的优惠.但是, ...
- python爬虫实践
模拟登陆与文件下载 爬取http://moodle.tipdm.com上面的视频并下载 模拟登陆 由于泰迪杯网站问题,测试之后发现无法用正常的账号密码登陆,这里会使用访客账号登陆. 我们先打开泰迪杯的 ...
随机推荐
- EnumSet详细讲解
https://blog.csdn.net/tugangkai/article/details/89631886 之前介绍的Set接口的实现类HashSet/TreeSet,它们内部都是用对应的Has ...
- Electron学习入门
1.安装electron,不建议全局安装,这样每个app可以使用不同的electron版本了 2.配置package.json中的script下的start属性的值为electron . Electr ...
- 22、vue实现随机四位数验证码
效果图: 1.新建生成验证码的组件Sidentify.vue(代码如下): <template> <div class="s-canvas"> <ca ...
- 【译】Matplotlib:plotting
前言 本教程源于Scipy Lecture Notes,URL:http://www.scipy-lectures.org/ 本教程若有翻译不当或概念不明之处,请大家留言,博主及时更正,以便后来的用户 ...
- 史上最全的整合第三方登录的工具JustAuth
JustAuth,如你所见,它仅仅是一个第三方授权登录的工具类库,它可以让我们脱离繁琐的第三方登录SDK,让登录变得So easy! 参考图例 授权gitee 授权github 授权weibo 授权钉 ...
- 关于连接不上SVN的部分解决方案——No repository found in svn localhost
今天如往常一样做事,期间发现一个问题,于是就打算将文件与 svn 上的文件进行对比,可谁成想 Eclipse 突然弹框报错,然后我到SVN资源库中直接刷新打开 svn 的地址,又弹框报错:文件夹不存在 ...
- nginx 常用的location rewrite proxy_pass
location 以 = 开头,表示精确匹配:如只匹配根目录结尾的请求,后面不能带任何字符串. 以^~ 开头,表示uri以某个常规字符串开头,如果匹配到,则不继续往下匹配.不是正则匹配 以~ 开头,表 ...
- Java开发环境之Gradle
查看更多Java开发环境配置,请点击<Java开发环境配置大全> 拾伍章:Gradle安装教程 1)下载Gradle安装包 官网下载:https://gradle.org/releases ...
- .Net core3.0 集成swagger5.0上传文件
.Net core 3.0已经更新了,相信有挺多博主大佬们都更新了如何在.Net core3.0使用swagger,这里就不详细说了. 我们知道,如果.net core 2.x使用swagger上传文 ...
- pyqt5--TableWidGet
使用pyqt5展示excel的数据到桌面,并获取选中的数据内容 from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtGui import Q ...