网页数据抓取怎么写(用Python写网络爬虫》——2.2三种网页抓取方法)

优采云 发布时间: 2022-01-12 14:18

  网页数据抓取怎么写(用Python写网络爬虫》——2.2三种网页抓取方法)

  摘要:这篇文章是关于使用Python实现网页数据抓取的三种方法;它们是正则表达式(re)、BeautifulSoup 模块和 lxml 模块。本文所有代码均在python3.5.

  中运行

   本文抓取的是[中央气象台](http://www.nmc.cn/)首页头条信息:

  

  它的 HTML 层次结构是:

  

  抓取href、title和tags的内容。

  一、正则表达式

  复制外层HTML:

  高温预警

  代码:

  # coding=utf-8import re, urllib.requesturl = 'http://www.nmc.cn'html = urllib.request.urlopen(url).read()html = html.decode('utf-8') #python3版本中需要加入links = re.findall('<a target="_blank" href="(.+?)" title'/span,html)titles = re.findall(span class="hljs-string"'a target="_blank" .+? title="(.+?)"'/span,html)tags = re.findall(span class="hljs-string"'a target="_blank" .+? title=.+?(.+?)/a'/span,html)span class="hljs-keyword"for/span span class="hljs-keyword"link/span,title,tag in zip(links,titles,tags): span class="hljs-keyword"print/span(tag,url+span class="hljs-keyword"link/span,title)/code/pre/p

p正则表达式符号'.'表示匹配任何字符串(\n除外); '+' 表示匹配0个或多个前面的正则表达式; '? ' 表示匹配 0 或 1 个之前的正则表达式。更多信息请参考Python正则表达式教程/p

p输出如下:/p

ppre class="prettyprint"code class=" hljs avrasm"高温预警 http://wwwspan class="hljs-preprocessor".nmc/spanspan class="hljs-preprocessor".cn/span/publish/country/warning/megatemperaturespan class="hljs-preprocessor".html/span 中央气象台span class="hljs-number"7/span月span class="hljs-number"13/span日span class="hljs-number"18/span时继续发布高温橙色预警山洪灾害气象预警 http://wwwspan class="hljs-preprocessor".nmc/spanspan class="hljs-preprocessor".cn/span/publish/mountainfloodspan class="hljs-preprocessor".html/span 水利部和中国气象局span class="hljs-number"7/span月span class="hljs-number"13/span日span class="hljs-number"18/span时联合发布山洪灾害气象预警强对流天气预警 http://wwwspan class="hljs-preprocessor".nmc/spanspan class="hljs-preprocessor".cn/span/publish/country/warning/strong_convectionspan class="hljs-preprocessor".html/span 中央气象台span class="hljs-number"7/span月span class="hljs-number"13/span日span class="hljs-number"18/span时继续发布强对流天气蓝色预警地质灾害气象风险预警 http://wwwspan class="hljs-preprocessor".nmc/spanspan class="hljs-preprocessor".cn/span/publish/geohazardspan class="hljs-preprocessor".html/span 国土资源部与中国气象局span class="hljs-number"7/span月span class="hljs-number"13/span日span class="hljs-number"18/span时联合发布地质灾害气象风险预警/code/pre/p

p二、BeautifulSoup 模块/p

pBeautiful Soup 是一个非常流行的 Python 模块。该模块可以解析网页并提供一个方便的界面来定位内容。/p

p复制选择器:/p

ppre class="prettyprint"code class=" hljs css"span class="hljs-id"#alarmtip/span span class="hljs-tag"ul/span span class="hljs-tag"li/spanspan class="hljs-class".waring/span span class="hljs-tag"a/spanspan class="hljs-pseudo":nth-child(1)/span/code/pre/p

p因为这里我们抓取的是多个数据,而不仅仅是第一个,所以需要改为:/p

ppre class="prettyprint"code class=" hljs vala"span class="hljs-preprocessor"#alarmtip ul li.waring a/span/code/pre/p

p代码:/p

ppre class="prettyprint"code class=" hljs lasso"from bs4 span class="hljs-keyword"import/span BeautifulSoupspan class="hljs-keyword"import/span urllibspan class="hljs-built_in"./spanrequesturl span class="hljs-subst"=/span span class="hljs-string"'http://www.nmc.cn'/spanhtml span class="hljs-subst"=/span urllibspan class="hljs-built_in"./spanrequestspan class="hljs-built_in"./spanurlopen(url)span class="hljs-built_in"./spanread()soup span class="hljs-subst"=/span BeautifulSoup(html,span class="hljs-string"'lxml'/span)content span class="hljs-subst"=/span soupspan class="hljs-built_in"./spanspan class="hljs-keyword"select/span(span class="hljs-string"'#alarmtip > ul > li.waring > a')for n in content: link = n.get('href') title = n.get('title') tag = n.text print(tag, url + link, title)

  输出结果同上。

  三、lxml 模块

  Lxml 是基于 XML 解析库 libxml2 的 Python 包装器。这个模块是用C语言编写的,解析速度比Beautiful Soup快,但是安装过程比较复杂。

  代码:

  import urllib.request,lxml.htmlurl = 'http://www.nmc.cn'html = urllib.request.urlopen(url).read()tree = lxml.html.fromstring(html)content = tree.cssselect('li.waring > a')for n in content: link = n.get('href') title = n.get('title') tag = n.text print(tag, url + link, title)

  输出结果同上。

  四、将抓取的数据存储在列表或字典中

  以 BeautifulSoup 模块为例:

  from bs4 import BeautifulSoupimport urllib.requesturl = 'http://www.nmc.cn'html = urllib.request.urlopen(url).read()soup = BeautifulSoup(html,'lxml')content = soup.select('#alarmtip > ul > li.waring > a')######### 添加到列表中link = []title = []tag = []for n in content: link.append(url+n.get('href')) title.append(n.get('title')) tag.append(n.text)######## 添加到字典中for n in content: data = { 'tag' : n.text, 'link' : url+n.get('href'), 'title' : n.get('title') }

  五、总结

  表格2.1总结了每种抓取方式的优缺点。

  

  参考资料:

  《用 Python 写一个网络爬虫》——2.2 三种网络爬取方法

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线