资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

python爬取网页函数,python怎么爬取网页数据

求python抓网页的代码

python3.x中使用urllib.request模块来抓取网页代码,通过urllib.request.urlopen函数取网页内容,获取的为数据流,通过read()函数把数字读取出来,再把读取的二进制数据通过decode函数解码(编号可以通过查看网页源代码中meta  http-equiv="content-type" content="text/html;charset=gbk" /得知,如下例中为gbk编码。),这样就得到了网页的源代码。

创新互联提供高防主机、云服务器、香港服务器、雅安服务器托管

如下例所示,抓取本页代码:

import urllib.request

html = urllib.request.urlopen('

).read().decode('gbk') #注意抓取后要按网页编码进行解码

print(html)

以下为urllib.request.urlopen函数说明:

urllib.request.urlopen(url,

data=None, [timeout, ]*, cafile=None, capath=None,

cadefault=False, context=None)

Open the URL url, which can be either a string or a Request object.

data must be a bytes object specifying additional data to be sent to

the server, or None

if no such data is needed. data may also be an iterable object and in

that case Content-Length value must be specified in the headers. Currently HTTP

requests are the only ones that use data; the HTTP request will be a

POST instead of a GET when the data parameter is provided.

data should be a buffer in the standard application/x-www-form-urlencoded format. The urllib.parse.urlencode() function takes a mapping or

sequence of 2-tuples and returns a string in this format. It should be encoded

to bytes before being used as the data parameter. The charset parameter

in Content-Type

header may be used to specify the encoding. If charset parameter is not sent

with the Content-Type header, the server following the HTTP 1.1 recommendation

may assume that the data is encoded in ISO-8859-1 encoding. It is advisable to

use charset parameter with encoding used in Content-Type header with the Request.

urllib.request module uses HTTP/1.1 and includes Connection:close header

in its HTTP requests.

The optional timeout parameter specifies a timeout in seconds for

blocking operations like the connection attempt (if not specified, the global

default timeout setting will be used). This actually only works for HTTP, HTTPS

and FTP connections.

If context is specified, it must be a ssl.SSLContext instance describing the various SSL

options. See HTTPSConnection for more details.

The optional cafile and capath parameters specify a set of

trusted CA certificates for HTTPS requests. cafile should point to a

single file containing a bundle of CA certificates, whereas capath

should point to a directory of hashed certificate files. More information can be

found in ssl.SSLContext.load_verify_locations().

The cadefault parameter is ignored.

For http and https urls, this function returns a http.client.HTTPResponse object which has the

following HTTPResponse

Objects methods.

For ftp, file, and data urls and requests explicitly handled by legacy URLopener and FancyURLopener classes, this function returns a

urllib.response.addinfourl object which can work as context manager and has methods such as

geturl() — return the URL of the resource retrieved,

commonly used to determine if a redirect was followed

info() — return the meta-information of the page, such

as headers, in the form of an email.message_from_string() instance (see Quick

Reference to HTTP Headers)

getcode() – return the HTTP status code of the response.

Raises URLError on errors.

Note that None

may be returned if no handler handles the request (though the default installed

global OpenerDirector uses UnknownHandler to ensure this never happens).

In addition, if proxy settings are detected (for example, when a *_proxy environment

variable like http_proxy is set), ProxyHandler is default installed and makes sure the

requests are handled through the proxy.

The legacy urllib.urlopen function from Python 2.6 and earlier has

been discontinued; urllib.request.urlopen() corresponds to the old

urllib2.urlopen.

Proxy handling, which was done by passing a dictionary parameter to urllib.urlopen, can be

obtained by using ProxyHandler objects.

Changed in version 3.2: cafile

and capath were added.

Changed in version 3.2: HTTPS virtual

hosts are now supported if possible (that is, if ssl.HAS_SNI is true).

New in version 3.2: data can be

an iterable object.

Changed in version 3.3: cadefault

was added.

Changed in version 3.4.3: context

was added.

怎么使用python来爬取网页上的表格信息

稍微说一下背景,当时我想研究蛋白质与小分子的复合物在空间三维结构上的一些规律,首先得有数据啊,数据从哪里来?就是从一个涵盖所有已经解析三维结构的蛋白质-小分子复合物的数据库里面下载。这时候,手动一个个去下显然是不可取的,我们需要写个脚本,能从特定的网站选择性得批量下载需要的信息。python是不错的选择。

import urllib #python中用于获取网站的模块

import urllib2, cookielib

有些网站访问时需要cookie的,python处理cookie代码如下:

cj = cookielib.CookieJar ( )

opener = urllib2.build_opener( urllib2.HttpCookieProcessor(cj) )

urllib2.install_opener (opener)

通常我们需要在网站中搜索得到我们需要的信息,这里分为二种情况:

1. 第一种,直接改变网址就可以得到你想要搜索的页面:

def GetWebPage( x ): #我们定义一个获取页面的函数,x 是用于呈递你在页面中搜索的内容的参数

url = ';' + ‘你想要搜索的参数’ # 结合自己页面情况适当修改

page = urllib2.urlopen(url)

pageContent = page.read( )

return pageContent #返回的是HTML格式的页面信息

2.第二种,你需要用到post方法,将你搜索的内容放在postdata里面,然后返回你需要的页面

def GetWebPage( x ): #我们定义一个获取页面的函数,x 是用于呈递你在页面中搜索的内容的参数

url = '' #这个网址是你进入搜索界面的网址

postData = urllib.urlencode( { 各种‘post’参数输入 } ) #这里面的post参数输入需要自己去查

req= urllib2.Request (url, postData)

pageContent = urllib2.urlopen (req). read( )

return pageContent #返回的是HTML格式的页面信息

在获取了我们需要的网页信息之后,我们需要从获得的网页中进一步获取我们需要的信息,这里我推荐使用 BeautifulSoup 这个模块, python自带的没有,可以自行百度谷歌下载安装。 BeautifulSoup 翻译就是‘美味的汤’,你需要做的是从一锅汤里面找到你喜欢吃的东西。

import re # 正则表达式,用于匹配字符

from bs4 import BeautifulSoup # 导入BeautifulSoup 模块

soup = BeautifulSoup(pageContent) #pageContent就是上面我们搜索得到的页面

soup就是 HTML 中所有的标签(tag)BeautifulSoup处理格式化后的字符串,一个标准的tag形式为:

hwkobe24

通过一些过滤方法,我们可以从soup中获取我们需要的信息:

(1) find_all ( name , attrs , recursive , text , **kwargs)

这里面,我们通过添加对标签的约束来获取需要的标签列表, 比如 soup.find_all ('p') 就是寻找名字为‘p’的 标签,而soup.find_all (class = "tittle") 就是找到所有class属性为"tittle" 的标签,以及soup.find_all ( class = re.compile('lass')) 表示 class属性中包含‘lass’的所有标签,这里用到了正则表达式(可以自己学习一下,非常有用滴)

当我们获取了所有想要标签的列表之后,遍历这个列表,再获取标签中你需要的内容,通常我们需要标签中的文字部分,也就是网页中显示出来的文字,代码如下:

tagList = soup.find_all (class="tittle") #如果标签比较复杂,可以用多个过滤条件使过滤更加严格

for tag in tagList:

print tag.text

f.write ( str(tag.text) ) #将这些信息写入本地文件中以后使用

(2)find( name , attrs , recursive , text , **kwargs )

它与 find_all( ) 方法唯一的区别是 find_all() 方法的返回结果是值包含一个元素的列表,而 find() 方法直接返回结果

(3)find_parents( ) find_parent( )

find_all() 和 find() 只搜索当前节点的所有子节点,孙子节点等. find_parents() 和 find_parent() 用来搜索当前节点的父辈节点,搜索方法与普通tag的搜索方法相同,搜索文档搜索文档包含的内容

(4)find_next_siblings() find_next_sibling()

这2个方法通过 .next_siblings 属性对当 tag 的所有后面解析的兄弟 tag 节点进代, find_next_siblings() 方法返回所有符合条件的后面的兄弟节点,find_next_sibling() 只返回符合条件的后面的第一个tag节点

(5)find_previous_siblings() find_previous_sibling()

这2个方法通过 .previous_siblings 属性对当前 tag 的前面解析的兄弟 tag 节点进行迭代, find_previous_siblings()方法返回所有符合条件的前面的兄弟节点, find_previous_sibling() 方法返回第一个符合条件的前面的兄弟节点

(6)find_all_next() find_next()

这2个方法通过 .next_elements 属性对当前 tag 的之后的 tag 和字符串进行迭代, find_all_next() 方法返回所有符合条件的节点, find_next() 方法返回第一个符合条件的节点

(7)find_all_previous() 和 find_previous()

这2个方法通过 .previous_elements 属性对当前节点前面的 tag 和字符串进行迭代, find_all_previous() 方法返回所有符合条件的节点, find_previous()方法返回第一个符合条件的节点

具体的使用方法还有很多,用到这里你应该可以解决大部分问题了,如果要更深入了解可以参考官方的使用说明哈!

如何用Python爬虫抓取网页内容?

爬虫流程

其实把网络爬虫抽象开来看,它无外乎包含如下几个步骤

模拟请求网页。模拟浏览器,打开目标网站。

获取数据。打开网站之后,就可以自动化的获取我们所需要的网站数据。

保存数据。拿到数据之后,需要持久化到本地文件或者数据库等存储设备中。

那么我们该如何使用 Python 来编写自己的爬虫程序呢,在这里我要重点介绍一个 Python 库:Requests。

Requests 使用

Requests 库是 Python 中发起 HTTP 请求的库,使用非常方便简单。

模拟发送 HTTP 请求

发送 GET 请求

当我们用浏览器打开豆瓣首页时,其实发送的最原始的请求就是 GET 请求

import requests

res = requests.get('')

print(res)

print(type(res))

Response [200]

class 'requests.models.Response'

怎样用python爬取网页

# coding=utf-8

import urllib

import re

# 百度贴吧网址:

# 根据URL获取网页HTML内容

def getHtmlContent(url):

page = urllib.urlopen(url)

return page.read()

# 从HTML中解析出所有jpg的图片的URL

# 从HTML中jpg格式为img ... src = "xxx.jpg" width='''

def getJPGs(html):

# 解析jpg图片URL的正则表达式

jpgReg = re.compile(r'img.+?src="(.+?\.jpg)"')

# 解析出jpg的URL列表

jpgs = re.findall(jpgReg, html)

return jpgs

# 用图片url下载图片 并保存成制定文件名

def downloadJPG(imgUrl, fileName):

urllib.urlretrieve(imgUrl, fileName)

# 批量下载图片,默认保存到当前目录下

def batchDownloadJPGs(imgUrls, path='../'):  # path='./'

# 给图片重命名

count = 1

for url in imgUrls:

downloadJPG(url, ''.join([path, '{0}.jpg'.format(count)]))

print "下载图片第:", count, "张"

count += 1

# 封装:从百度贴吧网页下载图片

def download(url):

html = getHtmlContent(url)

jpgs = getJPGs(html)

batchDownloadJPGs(jpgs)

def main():

url = ""

download(url)

if __name__ == '__main__':

main()

Python网页解析库:用requests-html爬取网页

Python 中可以进行网页解析的库有很多,常见的有 BeautifulSoup 和 lxml 等。在网上玩爬虫的文章通常都是介绍 BeautifulSoup 这个库,我平常也是常用这个库,最近用 Xpath 用得比较多,使用 BeautifulSoup 就不大习惯,很久之前就知道 Reitz 大神出了一个叫 Requests-HTML 的库,一直没有兴趣看,这回可算歹着机会用一下了。

使用 pip install requests-html 安装,上手和 Reitz 的其他库一样,轻松简单:

这个库是在 requests 库上实现的,r 得到的结果是 Response 对象下面的一个子类,多个一个 html 的属性。所以 requests 库的响应对象可以进行什么操作,这个 r 也都可以。如果需要解析网页,直接获取响应对象的 html 属性:

不得不膜拜 Reitz 大神太会组装技术了。实际上 HTMLSession 是继承自 requests.Session 这个核心类,然后将 requests.Session 类里的 requests 方法改写,返回自己的一个 HTMLResponse 对象,这个类又是继承自 requests.Response,只是多加了一个 _from_response 的方法来构造实例:

之后在 HTMLResponse 里定义属性方法 html,就可以通过 html 属性访问了,实现也就是组装 PyQuery 来干。核心的解析类也大多是使用 PyQuery 和 lxml 来做解析,简化了名称,挺讨巧的。

元素定位可以选择两种方式:

方法名非常简单,符合 Python 优雅的风格,这里不妨对这两种方式简单的说明:

定位到元素以后势必要获取元素里面的内容和属性相关数据,获取文本:

获取元素的属性:

还可以通过模式来匹配对应的内容:

这个功能看起来比较鸡肋,可以深入研究优化一下,说不定能在 github 上混个提交。

除了一些基础操作,这个库还提供了一些人性化的操作。比如一键获取网页的所有超链接,这对于整站爬虫应该是个福音,URL 管理比较方便:

内容页面通常都是分页的,一次抓取不了太多,这个库可以获取分页信息:

结果如下:

通过迭代器实现了智能发现分页,这个迭代器里面会用一个叫 _next 的方法,贴一段源码感受下:

通过查找 a 标签里面是否含有指定的文本来判断是不是有下一页,通常我们的下一页都会通过 下一页 或者 加载更多 来引导,他就是利用这个标志来进行判断。默认的以列表形式存在全局: ['next','more','older'] 。我个人认为这种方式非常不灵活,几乎没有扩展性。 感兴趣的可以往 github 上提交代码优化。

也许是考虑到了现在 js 的一些异步加载,这个库支持 js 运行时,官方说明如下:

使用非常简单,直接调用以下方法:

第一次使用的时候会下载 Chromium,不过国内你懂的,自己想办法去下吧,就不要等它自己下载了。render 函数可以使用 js 脚本来操作页面,滚动操作单独做了参数。这对于上拉加载等新式页面是非常友好的。

零基础学python(1)——爬取房天下网站信息

一、认识网页

网页分为三个部分:HTML(结构)、CSS(样式)、JavaScript(功能)。

二、爬取网站信息入门

1、Soup = BeautifulSoup (html, 'lxml'),使用beautifulsoup来解析网页。

2、使用copy CSS selector来复制网页元素的位置。

三、爬取房天下网站信息 

1、导入requests和beautifulsoup

2、定义函数spider_ftx,把所需要爬取的信息都定义出来

3、调用函数spider_ftx

4、翻页爬取二手房信息

由于每页最多只能显示40条信息,观察每一页网址的变化规律,写一个循环调用的语句,把全部100页的信息全都爬取下来。

四、小结:

目前只能爬取到网站的100页信息,网站为了反爬,设置了可浏览的页面量100。要想爬取网站的所有信息,可以通过分类去获取,但是如何用python实现呢,请看下集。


网站标题:python爬取网页函数,python怎么爬取网页数据
本文链接:http://cdkjz.cn/article/dschcos.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220