python网页数据抓取(Python中的正则表达式教程输出结果及总结表(一))

优采云 发布时间: 2022-02-01 23:06

  python网页数据抓取(Python中的正则表达式教程输出结果及总结表(一))

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

  中运行

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

  

  它的 HTML 层次结构是:

  

  抓取href、title和tags的内容。

  一、正则表达式

  复制外层HTML:

  高温预警

  代码:

  # coding=utf-8

import re, urllib.request

url = 'http://www.nmc.cn'

html = urllib.request.urlopen(url).read()

html = html.decode('utf-8') #python3版本中需要加入

links = re.findall(&#39;<a target="_blank" href="(.+?)" title&#39;/span,html)

titles = re.findall(span class="hljs-string"&#39;a target="_blank" .+? title="(.+?)"&#39;/span,html)

tags = re.findall(span class="hljs-string"&#39;a target="_blank" .+? title=.+?(.+?)/a&#39;/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 > ul > li.waring > a:nth-child(1)

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

  #alarmtip > ul > li.waring > a

  代码:

  from bs4 import BeautifulSoup

import urllib.request

url = &#39;http://www.nmc.cn&#39;

html = urllib.request.urlopen(url).read()

soup = BeautifulSoup(html,&#39;lxml&#39;)

content = soup.select(&#39;#alarmtip > ul > li.waring > a&#39;)

for n in content:

link = n.get(&#39;href&#39;)

title = n.get(&#39;title&#39;)

tag = n.text

print(tag, url + link, title)

  输出结果同上。

  三、lxml 模块

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

  代码:

  import urllib.request,lxml.html

url = &#39;http://www.nmc.cn&#39;

html = urllib.request.urlopen(url).read()

tree = lxml.html.fromstring(html)

content = tree.cssselect(&#39;li.waring > a&#39;)

for n in content:

link = n.get(&#39;href&#39;)

title = n.get(&#39;title&#39;)

tag = n.text

print(tag, url + link, title)

  输出结果同上。

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

  以 BeautifulSoup 模块为例:

  from bs4 import BeautifulSoup

import urllib.request

url = &#39;http://www.nmc.cn&#39;

html = urllib.request.urlopen(url).read()

soup = BeautifulSoup(html,&#39;lxml&#39;)

content = soup.select(&#39;#alarmtip > ul > li.waring > a&#39;)

######### 添加到列表中

link = []

title = []

tag = []

for n in content:

link.append(url+n.get(&#39;href&#39;))

title.append(n.get(&#39;title&#39;))

tag.append(n.text)

######## 添加到字典中

for n in content:

data = {

&#39;tag&#39; : n.text,

&#39;link&#39; : url+n.get(&#39;href&#39;),

&#39;title&#39; : n.get(&#39;title&#39;)

}

  五、总结

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

  

  源码链接

  参考资料:

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线