python爬取网页数据方法
#最基本,请求地址无参数
# response=urllib.request.urlopen("https://www.scetc.edu.cn")
#
# html=response.read().decode("utf-8")
#
# print(html)
#参数的转换 参数的原始数据
# key_value={'kw' : '胡歌'}
# #要使用urllib.parse模块下的urllencode对原始数据进行转换,并且encode进行编码
# data=bytes(urllib.parse.urlencode(key_value).encode('utf-8'))
#
# response=urllib.request.urlopen("http://tieba.baidu.com/f?",data=data)
#
# html=response.read().decode('utf-8')
# print(html)
#timeout是指等待响应的时间
print(html)
HttpResponse对象的三个参数属性
"""
print("响应的url:",back_url)
print("响应的状态码:",back_code)
print("响应的信息:",back_info)
构造Request对象
"""
import urllib.parse
header={"User-Agent":"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT6.1; Trident/5.0)"}
#发送请求参数数据
params={"news_id":174,"page":1}
data=bytes(urllib.parse.urlencode(params).encode('utf-8'))
#封装request对象
#地址
url="http://www.scetc.cn/index!detail"
request=urllib.request.Request(url,data=data,headers=header)
#连接类型
request.add_header("Connection", "keep-alive")
response=urllib.request.urlopen(request)
#代理ip
proxy_list=[
{"http": "124.88.67.81:80"},
{"http" : "127.88.67.81:80"},
{"http" : "121.82.67.81:80"},
{"http" : "124.55.67.81:80"},
{"http" : "124.56.67.81:80"},
{"http" : "124.78.67.81:80"},
]
#随机选取代理服务器地址
ran_proxy=random.choice(proxy_list)
#创建handler对象
httpproxy_handler = urllib.request.ProxyHandler(ran_proxy)
#获取opener对象
opener = urllib.request.build_opener(httpproxy_handler) #构建Request对象
header={"User-Agent":"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT6.1; Trident/5.0)"}
request=urllib.request.Request('http://www.scetc.net',headers=header)
#请求访问
response=opener.open(request)
#获取响应内容
html=response.read().decode('utf-8')
print(html)
python爬取网页数据方法的更多相关文章
- 使用 Python 爬取网页数据
1. 使用 urllib.request 获取网页 urllib 是 Python 內建的 HTTP 库, 使用 urllib 可以只需要很简单的步骤就能高效采集数据; 配合 Beautiful 等 ...
- python爬取网页数据
一.利用webbrowser.open()打开一个网站: ? 1 2 3 >>> import webbrowser >>> webbrowser.open('ht ...
- python爬取网页数据并存储到mysql数据库
#python 3.5 from urllib.request import urlopen from urllib.request import urlretrieve from bs4 impor ...
- 如何使用python爬取网页动态数据
我们在使用python爬取网页数据的时候,会遇到页面的数据是通过js脚本动态加载的情况,这时候我们就得模拟接口请求信息,根据接口返回结果来获取我们想要的数据. 以某电影网站为例:我们要获取到电影名称以 ...
- 使用webdriver+urllib爬取网页数据(模拟登陆,过验证码)
urilib是python的标准库,当我们使用Python爬取网页数据时,往往用的是urllib模块,通过调用urllib模块的urlopen(url)方法返回网页对象,并使用read()方法获得ur ...
- python爬取网站数据保存使用的方法
这篇文章主要介绍了使用Python从网上爬取特定属性数据保存的方法,其中解决了编码问题和如何使用正则匹配数据的方法,详情看下文 编码问题因为涉及到中文,所以必然地涉及到了编码的问题,这一次借这 ...
- python之爬取网页数据总结(一)
今天尝试使用python,爬取网页数据.因为python是新安装好的,所以要正常运行爬取数据的代码需要提前安装插件.分别为requests Beautifulsoup4 lxml 三个插件 ...
- python爬取网站数据
开学前接了一个任务,内容是从网上爬取特定属性的数据.正好之前学了python,练练手. 编码问题 因为涉及到中文,所以必然地涉及到了编码的问题,这一次借这个机会算是彻底搞清楚了. 问题要从文字的编码讲 ...
- 毕设之Python爬取天气数据及可视化分析
写在前面的一些P话:(https://jq.qq.com/?_wv=1027&k=RFkfeU8j) 天气预报我们每天都会关注,我们可以根据未来的天气增减衣物.安排出行,每天的气温.风速风向. ...
随机推荐
- python导包问题,这一篇就够了
解决办法: 将项目所在的根目录添加到sys.path中 在入口文件中加入如下代码: import sys import os # 将 项目的根目录添加到sys.path中 BASE_DIR = os. ...
- bootstrap富文本编辑
先把设定富文本框架 <div class="form-group"> <label class="col-sm-2 control-label" ...
- learning scala stripMargin
(1)Scala中创建多行字符串使用Scala的Multiline String. 在Scala中,利用三个双引号包围多行字符串就可以实现. 代码实例如: val foo = "" ...
- python对象调用父类的方法
#类定义 class People: #定义基本属性 name = '' age = 0 #定义私有属性,私有属性在类外部无法直接进行访问 __weight = 0 #定义构造方法 def __ini ...
- 使用scala通过JNI技术调用c++代码
scala代码编写 Sample1.scala class Sample1 { // --- Native methods @native def intMethod(n: Int): Int def ...
- mysqlslap压力测试时出现"Can't connect to MySQL server"
mysqlslap -utest -h 192.168.1.12 -p'test' --concurrency=100 --iterations=500 --create-schema='my_db' ...
- Hbase 错误记录分析(1) region超时问题
错误现象: 默认等待时间是60秒,超过这个时间就报超时问题了.因此需调整超时时间,默认为60秒,在配置文件 hbase-site.xml中: 调整成10分钟 <property> & ...
- Mybatis使用IN语句查询
一.简介 在SQL语法中如果我们想使用in的话直接可以像如下一样使用: select * from HealthCoupon where useType in ( '4' , '3' ) 但是如果在M ...
- ElasticSearch及其插件安装配置
elasticsearch安装使用 .安装步骤: 1.下载elasticsearch的rpm包: wget https://artifacts.elastic.co/downloads/elastic ...
- [ambari环境搭建](未完待续)
[安装] https://blog.csdn.net/Happy_Sunshine_Boy/article/details/86595945#commentBox https://www.jiansh ...