python获取网页数据_计算机毕业设计中Python爬取HTML网页数据

57534a899f9067c248a807e31a12274e.png点击上方“蓝字”,关注我们.

软件环境

  • Mac 10.13.1 (17B1003)

  • Python 2.7.10

  • VSCode 1.18.1

摘要

本文是练手Demo,主要是使用 Beautiful Soup 来爬取网页数据。

Beautiful Soup 介绍

Beautiful Soup提供一些简单的、python式的用来处理导航、搜索、修改分析树等功能。

Beautiful Soup 官方中文文档

特点

  • 简单:它是一个工具箱,通过解析文档为用户提供需要抓取的数据

  • Beautiful Soup自动将输入文档转换为Unicode编码,输出文档转换为utf-8编码。

Beautiful Soup 的安装

  • 安装 pip (如果需要): sudo easy_install pip

  • 安装 Beautiful Soup: sudo pip install beautifulsoup4

示例

本示例是抓取某公司的投资列表页面,页面如下图:9ec2eb63c42c811828e7d711bc782864.png

确定获取数据范围

本示例是获取项目列表,打开Chrome的调试栏,找到对应的位置,如下图:894b637e42da909b108d4825e53eeaaa.png

导包

import sys
import json
import urllib2 as HttpUtils
import urllib as UrlUtils
from bs4 import BeautifulSoup
  • 1

  • 2

  • 3

  • 4

  • 5

获取页面信息(分页)

def gethtml(page):
'获取指定页码的网页数据'
url = 'https://box.xxx.com/Project/List'
values = {
'category': '',
'rate': '',
'range': '',
'page': page
}
data = UrlUtils.urlencode(values)
# 使用 DebugLog
httphandler = HttpUtils.HTTPHandler(debuglevel=1)
httpshandler = HttpUtils.HTTPSHandler(debuglevel=1)
opener = HttpUtils.build_opener(httphandler, httpshandler)
HttpUtils.install_opener(opener)
request = HttpUtils.Request(url + '?' + data)
request.get_method = lambda: 'GET'
try:
response = HttpUtils.urlopen(request, timeout=10)
except HttpUtils.URLError, err:
if hasattr(err, 'code'):
print err.code
if hasattr(err, 'reason'):
print err.reason
return None
else:
print '====== Http request OK ======'
return response.read().decode('utf-8')
TIPS
  • urlopen(url, data, timeout)

    • url: 请求的 URL

    • data: 访问 URL 时要传送的数据

    • timeout: 超时时间

  • HttpUtils.build_opener(httphandler, httpshandler)

    • 开启日志,将会在调试控制台输出网络请求日志,方便调试

  • 必要的 try-catch,以便可以捕获到网络异常

解析获取的数据

创建BeautifulSoup对象
soup = BeautifulSoup(html, 'html.parser')
获取待遍历的对象
# items 是一个  对象,不是一个list,但是可以循环遍历所有子节点。
items = soup.find(attrs={'class':'row'}).children
遍历子节点,解析并获取所需参数
projectList = []
for item in items:
if item == '\n': continue
# 获取需要的数据
title = item.find(attrs={'class': 'title'}).string.strip()
projectId = item.find(attrs={'class': 'subtitle'}).string.strip()
projectType = item.find(attrs={'class': 'invest-item-subtitle'}).span.string
percent = item.find(attrs={'class': 'percent'})
state = 'Open'
if percent is None: # 融资已完成
percent = '100%'
state = 'Finished'
totalAmount = item.find(attrs={'class': 'project-info'}).span.string.strip()
investedAmount = totalAmount
else:
percent = percent.string.strip()
state = 'Open'
decimalList = item.find(attrs={'class': 'decimal-wrap'}).find_all(attrs={'class': 'decimal'})
totalAmount = decimalList[0].string
investedAmount = decimalList[1].string
investState = item.find(attrs={'class': 'invest-item-type'})
if investState != None:
state = investState.string
profitSpan = item.find(attrs={'class': 'invest-item-rate'}).find(attrs={'class': 'invest-item-profit'})
profit1 = profitSpan.next.strip()
profit2 = profitSpan.em.string.strip()
profit = profit1 + profit2
term = item.find(attrs={'class': 'invest-item-maturity'}).find(attrs={'class': 'invest-item-profit'}).string.strip()
project = {
'title': title,
'projectId': projectId,
'type': projectType,
'percent': percent,
'totalAmount': totalAmount,
'investedAmount': investedAmount,
'profit': profit,
'term': term,
'state': state
}
projectList.append(project)
输出解析结果,如下:

93330a5aad13e4e81b4c477c492016ef.png

TIPS
  • 解析html代码,主要是运用了BeautifulSoup的几大对象,Tag、NavigableString、BeautifulSoup、Comment,可以参考Beautiful Soup 官方中文文档

1eca27b1f218a0bc63adc315bdfd42ea.gif

更多请关注小编继续了解,免费赠送网页设计作业源代码哦!

如需要精品网页设计作业和毕业设计作品

联系QQ:2656895362进行咨询

506a77b0a7f8ea094b6f07e1e91b547a.png ec347b38b4f9c5387ef4384ecf9b6933.png 网站地址: http://www.85work.com/ 扫码关注最新动态更多精彩,点击下方“
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值