-
2020-11-21 02:25:59
我的第一个网页
更多相关内容 -
python获取整个网页源码的方法
2020-12-17 10:44:011、Python中获取整个页面的代码: import requests res = requests.get('https://blog.csdn.net/yirexiao/article/details/79092355') res.encoding = 'utf-8' print(res.text) 2、运行结果 实例扩展: ... -
4.1 使用Python获取网页源代码
2021-03-30 21:25:022)使用requests获取网页源代码 a. GET方式 import requests html = requests.get('网址')#得到一个Response对象 html_bytes = html.content#属性.content用来显示bytes型网页的源代码 html_str = html_bytes.dec1)第三方库的安装
a.在线安装
pip install 第三方库名
b.本地安装
下载对应版本的.whl文件,然后cd到文件目录下,通过pip install xxx.whl
2)使用requests获取网页源代码
a. GET方式
import requests html = requests.get('网址')#得到一个Response对象 html_bytes = html.content#属性.content用来显示bytes型网页的源代码 html_str = html_bytes.decode()#属性.decode()用来把bytes型的数据解码为字符串型的数据,默认编码格式UTF-8
常见的编码格式 UTF-8、GBK、GB2312、GB18030。以中文可以正常显示为准。
上面的代码可缩减为:html_str = requests.get('网址').content.decode()
b. POST方式
有些网页使用GET和POST方式访问同样的网址,得到的结果不一样。还有些网页只能用POST方式访问,使用GET方式访问返回错误信息。
post()方法的格式:import requests data = {'key1':'value1','key2':'value2'} html_formdata = requests.post('网址',data = data).content.decode() #html_formdata = requests.post('网址',json = data).content.decode()#有些网址提交的内容是json格式
3)结合requests与正则表达式
①提取标题title = re.search('title>(.*?)<',html,re.S).group(1)
②提取正文,并将两端正文使用换行符拼接起来
content_list = re.findall('p>(.*?)<', html_str,re.S) content_str = '\n'.join(content_list)
完整代码如下:
import requests import re html_str = requests.get('http://exercise.kingname.info/exercise_requests_get.html').content.decode() title = re.search('title>(.*?)<',html,re.S).group(1) content_list = re.findall('p>(.*?)<', html_str,re.S) content_str = '\n'.join(content_list) print(f'页面标题为:{title}') print(f'页面正文内容为:\n{content_str}')
总结
- 建议安装第三方库时使用本地安装,因为有些库在线安装传输速度非常慢。
- 网页源代码获取格式
#GET方式 html_str = requests.get('网址').content.decode(编码格式,默认UTF-8)
#POST方式 data json html_str = requests.post('网址',data = data).content.decode() html_str = requests.post('网址',json = data).content.decode()
-
python 获取网页源代码
2020-11-29 12:54:52'__main__':getHtml('http://www.baidu.com/') 第三种获取网页源代码方法 import requests req = requests.get("http://news.sina.com.cn/") if req.encoding == 'ISO-8859-1':encodings = requests.utils.get_...import re #正则表达式模块
import urllib.request
import time #时间模块
import string #字符串模块
def getHtml(url):f=urllib.request.urlopen(url)
print(f.read())
if name == '__main__':
getHtml('http://www.baidu.com/')
下面的是获取网页代码不乱码
import re #正则表达式模块
import urllib.request
import time #时间模块
import string #字符串模块
def getHtml(url):f=urllib.request.urlopen(url)
print(str(f.read(),'utf-8'))
if name == '__main__':getHtml('http://www.baidu.com/')
第三种获取网页源代码方法
import requests
req = requests.get("http://news.sina.com.cn/")
if req.encoding == 'ISO-8859-1':encodings = requests.utils.get_encodings_from_content(req.text)
if encodings:
encoding = encodings[0]
else:
encoding = req.apparent_encoding
# encode_content = req.content.decode(encoding, 'replace').encode('utf-8', 'replace')
global encode_content
encode_content = req.content.decode(encoding, 'replace') #如果设置为replace,则会用?取代非法字符;
print(encode_content)
with open('test.html','w',encoding='utf-8') as f:f.write(encode_content)
赏
支付宝打赏
微信打赏
如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!
-
Python爬虫获取网页源代码出现乱码
2022-01-11 19:47:58通过如下代码,会发现获取的网页源代码出现乱码 url = 'https://www.baidu.com' res = requests.get(url).text print(res) 出现乱码 查看python获得的编码格式 import requests # 0.通过如下代码,会发现获取的...发现用python用requests在百度中获得的代码有乱码
import requests # 0.通过如下代码,会发现获取的网页源代码出现乱码 url = 'https://www.baidu.com' res = requests.get(url).text print(res)
出现乱码查看python获得的编码格式
import requests # 0.通过如下代码,会发现获取的网页源代码出现乱码 url = 'https://www.baidu.com' res = requests.get(url).text print(res) # 1.编码分析 # 1.1 查看Python获得的网页源代码的编码方式,其编码方式为ISO-8859-1 url = 'https://www.baidu.com' code = requests.get(url).encoding print('通过Python获得的网页源代码的编码方式为:' + code)
网页获取的格式按F12在head中查看
发现为utf-8编码需要转码import requests # 0.通过如下代码,会发现获取的网页源代码出现乱码 url = 'https://www.baidu.com' res = requests.get(url).text print(res) # 1.编码分析 # 1.1 查看Python获得的网页源代码的编码方式,其编码方式为ISO-8859-1 url = 'https://www.baidu.com' code = requests.get(url).encoding print('通过Python获得的网页源代码的编码方式为:' + code) # 1.2 查看网页实际的编码方式,通过F12查看,展开最上方的head标签(head标签里主要用来存储编码方式、网站标题等信息), # 其中<meta charset="编码方式">中存储着网页实际的编码方式,可以看到网页实际的编码方法为utf-8 # 2.重新编码及解码 url = 'https://www.baidu.com' res = requests.get(url).text res = res.encode('ISO-8859-1').decode('utf-8') print(res) # 此时便已经可以解决数据乱码问题了
还有gbk的编码格式
其它情况的完整代码如下:
# ============================================================================= # 5.2 数据乱码常规处理方法 by 王宇韬 代码更新:www.huaxiaozhi.com 资料下载区 # ============================================================================= import requests # 0.通过如下代码,会发现获取的网页源代码出现乱码 url = 'https://www.baidu.com' res = requests.get(url).text print(res) # 1.编码分析 # 1.1 查看Python获得的网页源代码的编码方式,其编码方式为ISO-8859-1 url = 'https://www.baidu.com' code = requests.get(url).encoding print('通过Python获得的网页源代码的编码方式为:' + code) # 1.2 查看网页实际的编码方式,通过F12查看,展开最上方的head标签(head标签里主要用来存储编码方式、网站标题等信息), # 其中<meta charset="编码方式">中存储着网页实际的编码方式,可以看到网页实际的编码方法为utf-8 # 2.重新编码及解码 url = 'https://www.baidu.com' res = requests.get(url).text res = res.encode('ISO-8859-1').decode('utf-8') print(res) # 此时便已经可以解决数据乱码问题了 # 注意,如果有的网站的实际编码方式为gbk,则在decode解码的时候需要把utf-8换成gbk # 补充知识点:encode()编码函数与decode()解码函数 # encode()编码函数 res = '华小智' # 中文字符串 res = res.encode('utf-8') # encode编码将中文字符串转为二进制 print(res) # decode()解码函数 res = b'\xe5\x8d\x8e\xe5\xb0\x8f\xe6\x99\xba' # 二进制字符 res = res.decode('utf-8') # decode解码将二进制字符转为字符串 print(res) # 3.数据乱码万金油的解决办法 import requests headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'} url = 'https://www.baidu.com' res = requests.get(url).text # res = requests.get(url, headers=headers).text # 这里加headers能获取更多的网页源代码 try: res = res.encode('ISO-8859-1').decode('utf-8') # 方法3 except: try: res = res.encode('ISO-8859-1').decode('gbk') # 方法2 except: res = res # 方法1 print(res) # 可以在源代码里搜索“百度”检验爬取成功
-
Python爬虫1-获取指定网页源码
2018-10-22 21:23:291、任务简介 前段时间一直在学习Python基础知识,故未更新博客,...Python获取指定网页源码的方法较为简单,我在Java中使用了38行代码才获取了网页源码(大概是学艺不精),而Python中只用了6行就达到了效果。 Pyt... -
python获取网页内容.zip
2020-07-02 10:22:39用python爬取特定网站URL的文章,并保存到本地的自定义格式的TXT文件中(额外:利用网站主页获取特定的URL列表写到TXT文件) -
使用selenium webdriver python获取页面源代码
2020-11-25 23:09:44这是我的代码移动到所有的页面并获取它们的页面源代码。但函数末尾没有打印或返回。我是为其他网站做的,但不是这里。请帮我摆脱困境。谢谢你def get_html(driver):output = []keep_going = Truewhile keep_going:# ... -
python获取指定网页上所有超链接的方法
2020-09-22 07:11:26主要介绍了python获取指定网页上所有超链接的方法,涉及Python使用urllib2模块操作网页抓取的技巧,非常具有实用价值,需要的朋友可以参考下 -
python获取网页源代码
2019-08-05 21:06:00最简单的网页取源(不用模拟浏览器的情况) 1 import requests 2 def getHTML(url): 3 try: 4 r = requests.get(url,timeout=30) 5 r.raise_for_status() 6 r.encoding = 'utf-8' 7 ... -
python学习笔记(二)---python爬取网页源代码
2022-01-17 18:59:42python学习笔记(二)—python爬取网页源代码 使用模块urllib #coding:utf-8 import urllib.request 请求url,获取网页源代码 def getHtml(url): h = urllib.request.urlopen(url).read() return h 保存文档 def... -
python selenium 获取网页源代码
2021-06-21 15:19:17获取网页源代码: 引入组件:【from selenium import webdriver】、 【from selenium.webdriver.chrome.options import Options】 构建浏览器链接: chrome_options = Options() chrome_options.add_argument('--... -
python爬取的源代码和检查的网页源代码不一致
2021-04-09 16:40:37python爬取的代码,img标签中的src属性值是这样的,无法打开 ctrl+u查看的网页源代码,img标签src属性是正常的,可以打开,而且除了src属性,还有其他属性 f12检查里查看的源代码和ctrl+u查看到的源代码一致,不知道... -
Python抓取网页信息时,读取的是非网页源代码,怎么解决啊
2021-02-04 05:55:23读取的网页是:http://acm.nyist.net/我的代码是:importurlliburl="http://acm.nyist.net"printurllib.urlopen(url).read()#.decode('gbk','ignore').encode('utf-8')Python2.7.6(de...读取的网页是:... -
5 实战1—利用Python获取新闻网页源代码
2021-08-04 14:25:10利用Python获取新闻网页源代码 通过Requests库来尝试获取百度新闻的网页源代码 import requests url = 'https://www.baidu.com/s?tn=news&rtt=1&bsst=1&cl=2&wd=阿里巴巴' res = requests.get(url, ... -
Python读取网页内容的方法
2021-02-09 13:52:06本文实例讲述了Python读取网页内容的方法。分享给大家供大家参考。具体如下:import urllib2#encoding = utf-8class Crawler:def main(self):#req = urllib2.Request('http://www.baidu.com/')#req.add_header('User... -
用python打印网页源代码
2021-02-09 11:41:47我刚刚在使用Python3.2.5的Win7上尝试了同样的方法,下面是我得到的:Python 3.2.5 (default, May 15 2013, 23:07:10) [MSC v.1500 64 bit (AMD64)] on win32Type "help", "copyright", "credits" or "license" for ... -
使用python爬取网站源代码
2021-02-23 12:47:14常用代码 -
python 爬取csdn网页并保存博客到本地
2021-03-05 14:55:42这几天一直在学用python爬网页 , 现在是用urllib2,cookie等模块获取了csdn的博客源码,然后打算把所有博客都保存到本地;这就涉及到了解析html, 由于对正则的理解不太深。。。就用了第三方工具模块:美丽的汤---... -
python获取网页源码,经过伪装,自动判断网页压缩与否
2015-06-18 15:23:34相对来说简单可靠的方式获取网页源码,经过浏览器伪装,可采集压缩和未压缩的网页。 -
Python requests获取网页常用方法解析
2020-09-17 21:51:29主要介绍了Python requests获取网页常用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 -
python获取网页源代码后在网站上显示
2017-08-21 12:06:55思考了很多方法,比如jsonp跨域,script等等,但发现传回来的都是网页源代码,而我们的要求是打开这个页面,后来又利用了python的webbroswer,js的弹窗,又发现不能设置header和利用cookie登录,真是麻烦. 最后,直接... -
Python获取网页源码
2016-07-07 19:32:00Py2k中直接导入urllib2,就可以读取网页源码。import urllib2 content = urllib2.urlopen('http://www.baidu.com/').read() print(content) Py3k中取消了urllib2,需要导入urllib.request,等同于Py2k中的urllib2,... -
python3 爬取网页表格实例
2018-07-01 16:18:18python爬取网页的表格内容, 并存入csv文件, 网页地址:http://app.finance.ifeng.com/data/stock/yjyg.php?symbol=000001 -
【python】python获取网站源码失败,出现一堆script脚本内容
2022-02-13 16:50:24问题原因 网站开启了防爬虫,爬取的设置,通过头部的cookie和User-Agent判断 解决流程 在头部设置以下内容 def ask_url(url,method): proxies = { ... "Cookie":"xxx",//通过浏览器控制台获取 'User-A