爬虫抓取网页数据(Python模块的安装方法和使用难度-快-困难-简单)
优采云 发布时间: 2021-12-28 14:06爬虫抓取网页数据(Python模块的安装方法和使用难度-快-困难-简单)
我们需要让爬虫从每个网页中提取一些数据,然后实现某些事情,这种方式叫做数据抓取。
分析网页
查看网页源代码并使用 Firebug Lite 扩展。Firebug 是由 Joe Hewitt 开发的与 Firefox 集成的强大 Web 开发工具。它可以实时编辑、调试和监控任何页面的 CSS、HTML 和 JavaScript。这里用来查看网页的源代码。
安装 Firebug Lite,下载 Firebug Lite 包,然后在浏览器中安装插件。
网页抓取的三种方法
正则表达式,BeatifulSoup 模板,强大的 lxml 模块
正则表达式
def download(url,user_agent=\'wswp\',proxy=None,num_retries=2):
print \'Downloading:\',url
headers={\'User-agent\':user_agent}
request=urllib2.Request(url,headers=headers)
opener=urllib2.build_opener()
if opener:
proxy_params={urlparse.urlparse(url).scheme:proxy}
opener.add_handler(urllib2.ProxyHandler(proxy_params))
try:
html=urllib2.urlopen(request).read()
except urllib2.URLError as e:
print \'Download:\' ,e.reason
html=None
if num_retries>0:
if hasattr(e,\'code\') and 500
Area
Population
"""
测试三种方法的性能
<p>import re
import urllib2
import urlparse
from bs4 import BeautifulSoup
import lxml.html
import time
#
#
#
#获取网页内容
def download(url,user_agent=\'wswp\',proxy=None,num_retries=2):
print \'Downloading:\',url
headers={\'User-agent\':user_agent}
request=urllib2.Request(url,headers=headers)
opener=urllib2.build_opener()
if opener:
proxy_params={urlparse.urlparse(url).scheme:proxy}
opener.add_handler(urllib2.ProxyHandler(proxy_params))
try:
html=urllib2.urlopen(request).read()
except urllib2.URLError as e:
print \'Download:\' ,e.reason
html=None
if num_retries>0:
if hasattr(e,\'code\') and 500