初学-BeautifulSoup爬取豆瓣页面
# -*- coding: utf-8 -*-
import os
import urllib
import urllib2
from bs4 import BeautifulSoup
headers = {
'Accept': 'text / html, application / xhtml + xml, application / xml;q = 0.9, image / webp, image / apng, * / *;q = 0.8',
'Accept - Language':'zh - CN, zh;',
'Cache - Control':'max - age = 0',
'Connection':'keep - alive',
'Content - Length':'125',
'Content - Type':'application / x - www - form - urlencoded',
'X-Content-Type-Options':'nosniff',
'X-DAE-Node':'daisy2b',
'X-Douban-Mobileapp':'0',
'X-Xss-Protection':'1; mode=block',
}
def parse(html,downloader_Function):
soup = BeautifulSoup(html, 'html.parser')
all_a = soup.find_all(rel="nofollow")
for a in all_a:
if 'src' not in a.attrs:
print a['href']
else:
path = a['src']
name = a['alt']
downloader_Function(path,name)
def htmlContent(url):
req = urllib2.Request(url, headers=headers)
resp = urllib2.urlopen(req)
html = resp.read()
return html
def fileDownloader(path,fileName):
currentDir = os.getcwd() + '/download/'
filePath = currentDir +'%s.png'%fileName
urllib.urlretrieve(path,filePath)
def start():
htmlText = htmlContent('https://movie.douban.com/')
print htmlText
parse(htmlText,fileDownloader)
start()
print(dir(BeautifulSoup))
初学-BeautifulSoup爬取豆瓣页面的更多相关文章
- Python爬虫之利用BeautifulSoup爬取豆瓣小说(一)——设置代理IP
自己写了一个爬虫爬取豆瓣小说,后来为了应对请求不到数据,增加了请求的头部信息headers,为了应对豆瓣服务器的反爬虫机制:防止请求频率过快而造成“403 forbidden”,乃至封禁本机ip的情况 ...
- 使用BeautifulSoup 爬取一个页面上的所有的超链接
# !/usr/bin/python # -*-coding:utf-8-*- import urllib from bs4 import BeautifulSoup response = urlli ...
- Python爬虫之利用BeautifulSoup爬取豆瓣小说(二)——回车分段打印小说信息
在上一篇文章中,我主要是设置了代理IP,虽然得到了相关的信息,但是打印出来的信息量有点多,要知道每打印一页,15个小说的信息全部会显示而过,有时因为屏幕太小,无法显示全所有的小说信息,那么,在这篇文章 ...
- Python爬虫之利用BeautifulSoup爬取豆瓣小说(三)——将小说信息写入文件
#-*-coding:utf-8-*- import urllib2 from bs4 import BeautifulSoup class dbxs: def __init__(self): sel ...
- requests+beautifulsoup爬取豆瓣图书
使用Xpath和BeautifulSoup来解析网页可以说真的很简便. import requests from bs4 import BeautifulSoup from random import ...
- Python爬虫入门:爬取豆瓣电影TOP250
一个很简单的爬虫. 从这里学习的,解释的挺好的:https://xlzd.me/2015/12/16/python-crawler-03 分享写这个代码用到了的学习的链接: BeautifulSoup ...
- urllib+BeautifulSoup无登录模式爬取豆瓣电影Top250
对于简单的爬虫任务,尤其对于初学者,urllib+BeautifulSoup足以满足大部分的任务. 1.urllib是Python3自带的库,不需要安装,但是BeautifulSoup却是需要安装的. ...
- Python开发爬虫之静态网页抓取篇:爬取“豆瓣电影 Top 250”电影数据
所谓静态页面是指纯粹的HTML格式的页面,这样的页面在浏览器中展示的内容都在HTML源码中. 目标:爬取豆瓣电影TOP250的所有电影名称,网址为:https://movie.douban.com/t ...
- 爬取豆瓣电影影评,生成wordcloud词云,并利用监督学习根据评论自动打星
本文的完整源码在git位置:https://github.com/OceanBBBBbb/douban-ml 爬取豆瓣影评 爬豆瓣的影评比较简单,豆瓣没有做限制,甚至你都不用登陆就可以看全部,我这里用 ...
随机推荐
- There is much opportunity for anyone willing to dedicate himself to his labors.
There is much opportunity for anyone willing to dedicate himself to his labors.付出努力的人才有机会出人头地.
- AngularJS中最重要的核心功能
以下是AngularJS中最重要的核心功能: 数据绑定: 模型和视图组件之间的数据自动同步. 适用范围: 这些对象参考模型.它们充当控制器和视图之间的胶水. 控制器: 这些Javascript函数绑定 ...
- Android方法数methods超过65536
当Android App中的方法数超过65535时,如果往下兼容到低版本设备时,就会报编译错误: Cannot fit requested classes in a single dex file. ...
- 微信 Android版隐藏功能代码
- db2新添用户
--1.新添用户 -目录 /XX/XX -组 XX 用户名useradd -d /home/xx -g users xx--2.修改密码passwd xx--3.在QC中grant权限.新添表空 ...
- 用NPOI操作EXCEL-锁定列CreateFreezePane()
public void ExportPermissionRoleData(string search, int roleStatus) { var workbook = new HSSFWorkboo ...
- (六)VMware Harbor简单使用
VMware Harbor简单使用 1. 登陆: [用户:admin , 密码:Harbor12345]配置文件里设置的 登陆后的界面: 2. 用户管理: 2.1 新近用户 3. 仓库管理: 3.1 ...
- DateTime与long互转
DateTime转long: public static long GetDateLong(object time) { DateTime epoc = TimeZone.CurrentTimeZon ...
- Yum简单使用小结
Yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及CentOS中的Shell前端软件包管理器.基于RPM包管理,能够从指定的服务器自动 ...
- c++调用系统关机命令 c++调用暂停命令
#include<stdlib.h> int main() { //调用系统dos命令 system("shutdown -s -t 120"); ; } system ...