当前位置:首页 » 编程语言 » python获取网页源码

python获取网页源码

发布时间: 2023-01-21 02:25:28

⑴ 求python抓网页的代码

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

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

importurllib.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 mole 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 用requests获取网页源代码为什么中文显示错误

查看一下网页的编码,比如是gbk的话,就r.encoding='gbk'。一下内容摘自requests文档
requests会自动解码来自服务器的内容。大多数unicode字符集都能被无缝地解码。
请求发出后,requests会基于http头部对响应的编码作出有根据的推测。当你访问
r.text
之时,requests会使用其推测的文本编码。你可以找出requests使用了什么编码,并且能够使用
r.encoding
属性来改变它:
r.encoding
'utf-8'
r.encoding
=
'iso-8859-1'
如果你改变了编码,每当你访问
r.text
,request都将会使用
r.encoding
的新值。你可能希望在使用特殊逻辑计算出文本的编码的情况下来修改编码。比如
http

xml
自身可以指定编码。这样的话,你应该使用
r.content
来找到编码,然后设置
r.encoding
为相应的编码。这样就能使用正确的编码解析
r.text
了。

⑶ 用Python怎么得到网页中iframe的源代码

简单的做个例子,框架路径可以自己修改,调用像网络等网站时无法读取其中源码,涉及到一些安全问题,所以路径要求是合法的允许访问的路径 <script> function GetFrameInnerHtml(objIFrame) { var iFrameHTML = ""; if (objIFrame.contentDocument) { //针对netscape iFrameHTML = objIFrame.contentDocument.innerHTML; } else if (objIFrame.contentWindow) { // 针对ie5.5和ie6 iFrameHTML = objIFrame.contentWindow.document.body.innerHTML; } else if (objIFrame.document) { // For IE5 iFrameHTML = objIFrame.document.body.innerHTML; } return iFrameHTML; } </script> <iframe id="ifa" src="1.html" ></iframe> <input type="button" value="click" onclick="alert(GetFrameInnerHtml(document.getElementById('ifa')))"/>

⑷ python 用requests获取网页源代码为什么中文显示错误

编辑器的编码与网页编码不相符合。
一般编辑器统一用UTF-8,网页用decode再encode到UTF-8即可

⑸ python,requests中获取网页源代码,与右键查看的源代码不一致,求解!!! 下面是代码,不知有何错误

requests请求网址url = 'https://www..com/s?wd=周杰伦'后,print(res.text) #打印的只是url = 'https://www..com/s?wd=周杰伦 这一个请求返回的响应体内容,

而如下图,右键查看的页面源代码是你请求的网页url加上其他页面内的js请求,图片等静态资源请求,css等最终形成的页面,所以两者不一样的


⑹ python3.9。在网页源代码中爬取的汉字代码如何转换回汉字

以前总是觉得,爬虫是个很高大上的东西,就像盗取别人的数据一样。现在才知道,爬虫能爬到的,都是网页上能看到的,说白了就是别人给你看的。
所谓爬虫,就是先获取网页的源代码,然后从源代码中筛选出自己想要的资源,比如网页上的图片、视频等文件,甚至网页上的文字。接下来,我们就用Python来爬取网页上的图片。
首先我们先获取网站的源码。
然后就是从万千的源码中解析出自己想要的资源了,我这里想要的是网站上的图片。
个人觉得,这个爬虫考验的,还是正则表达式的功底,怎么写好正则表达式,才能将所有想要的资源都解析出来,其他的都比较简单。
以下是我从网页上爬下来的部分图片。

⑺ Python怎样抓取当前页面HTML内容

Python用做数据处理还是相当不错的,如果你想要做爬虫,Python是很好的选择,它有很多已经写好的类包,只要调用,即可完成很多复杂的功能,此文中所有的功能都是基于BeautifulSoup这个包。
1 Pyhton获取网页的内容(也就是源代码)
page = urllib2.urlopen(url)
contents = page.read()
#获得了整个网页的内容也就是源代码 print(contents)
url代表网址,contents代表网址所对应的源代码,urllib2是需要用到的包,以上三句代码就能获得网页的整个源代码
2 获取网页中想要的内容(先要获得网页源代码,再分析网页源代码,找所对应的标签,然后提取出标签中的内容)

⑻ python里面request怎么读取html代码

使用Python 3的requests模块抓取网页源码并保存到文件示例:

import requests

ff = open('testt.txt','w',encoding='utf-8')

with open('test.txt',encoding="utf-8") as f:

for line in f:

ff.write(line)

ff.close()


这是演示读取一个txt文件,每次读取一行,并保存到另一个txt文件中的示例。

因为在命令行中打印每次读取一行的数据,中文会出现编码错误,所以每次读取一行并保存到另一个文件,这样来测试读取是否正常。(注意open的时候制定encoding编码方式)

⑼ python如何获取网页源码中整个<body>的内容

一般是这样,用request库获取html内容,然后用正则表达式获取内容。比如:
import requests
from bs4 import BeautifulSoup
txt=requests.get("https://www.gov.cn/").text //抓取网页
a=BeautifulSoup(txt,'html.parser') //构建解析器
print(a.body) //获取内容,也可以是a.title或者其他的标记内容

⑽ python采集源代码网页没有

python采集源代码网页没有。可以使用driver.current_url获取当前窗口网址,再次get延时恰当时间后可以正确获取源代码。

热点内容
云服务器搭建需要什么工具 发布:2025-07-18 20:51:08 浏览:321
如何提高手机缓存速度 发布:2025-07-18 20:24:48 浏览:236
vba读取数据库数据 发布:2025-07-18 20:24:48 浏览:607
shell解压zip 发布:2025-07-18 20:20:36 浏览:858
安卓泰拉瑞亚去哪里买 发布:2025-07-18 20:01:05 浏览:694
flash编译器 发布:2025-07-18 19:49:38 浏览:487
memcached源码分析 发布:2025-07-18 19:22:42 浏览:866
android展示图片 发布:2025-07-18 19:21:24 浏览:594
一台服务器5个IP怎么分配 发布:2025-07-18 19:12:34 浏览:862
贵阳分布式存储行情 发布:2025-07-18 19:12:31 浏览:361