excel抓取多页网页数据(安装pip-Python的安装包管理工具)
优采云 发布时间: 2021-12-25 13:07excel抓取多页网页数据(安装pip-Python的安装包管理工具)
安装pip-Python安装包管理工具
Mac有自己的Python,我的mac系统是Sierra,自己的python版本是Python 2.7.13
sudo easy_install pip
相关工具安装:
1、网络请求工具
pip install lxml pip 安装请求
2、网页数据分析工具
pip install beautifulsoup4
3、解析器
pip 安装 html5lib
示例1:获取我的短书首页显示的所有文章标题
( )
查看网页元素如下:
查看网页元素
Python 代码展示:
from lxml import html
from lxml import etree
from urllib import urlopen
import requests
import bs4
from bs4 import BeautifulSoup
import html5lib
//网页数据获取
examplePage = urlopen('http://www.jianshu.com/u/5b771dd604fd')
//HTML数据
soupExam = BeautifulSoup(examplePage,"html5lib")
//网页标题
print soupExam.title
print soupExam.title.string
//文章标题
for link in soupExam.find_all('a',class_ = 'title'):
print(link.text)
输出结果如下:
输出结果
示例二:部分网站存在以下问题
1、想获取红标处的数据:
期望值
2、但是你得到的是文本中的文本内容:
结果值
问题原因如下:
(1)后台脚本请求网络数据,需要账号相关数据,解决办法是添加cookies;
(2)网页有刷新机制,首先获取的数据处于刷新状态,解决方法是休眠一段时间;