"""
#最基本,请求地址无参数
# 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是指等待响应的时间
response=urllib.request.urlopen("http://www.scetc.cn",timeout=5)
html=response.read().decode('utf-8')
print(html)
 
import urllib.request
"""
HttpResponse对象的三个参数属性
"""
response=urllib.request.urlopen("https://www.tmall.com")
back_url=response.geturl()
print("响应的url:",back_url)
back_code=response.getcode();
print("响应的状态码:",back_code)
back_info=response.info()
print("响应的信息:",back_info)
 
"""
构造Request对象
"""
import urllib.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")
#封装完毕之后openurl方法只需要传入这个Request对象就可以了
response=urllib.request.urlopen(request)
html=response.read().decode('utf-8')
print(html)
 
 
#代理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爬取网页数据方法的更多相关文章

  1. 使用 Python 爬取网页数据

    1. 使用 urllib.request 获取网页 urllib 是 Python 內建的 HTTP 库, 使用 urllib 可以只需要很简单的步骤就能高效采集数据; 配合 Beautiful 等 ...

  2. python爬取网页数据

    一.利用webbrowser.open()打开一个网站: ? 1 2 3 >>> import webbrowser >>> webbrowser.open('ht ...

  3. python爬取网页数据并存储到mysql数据库

    #python 3.5 from urllib.request import urlopen from urllib.request import urlretrieve from bs4 impor ...

  4. 如何使用python爬取网页动态数据

    我们在使用python爬取网页数据的时候,会遇到页面的数据是通过js脚本动态加载的情况,这时候我们就得模拟接口请求信息,根据接口返回结果来获取我们想要的数据. 以某电影网站为例:我们要获取到电影名称以 ...

  5. 使用webdriver+urllib爬取网页数据(模拟登陆,过验证码)

    urilib是python的标准库,当我们使用Python爬取网页数据时,往往用的是urllib模块,通过调用urllib模块的urlopen(url)方法返回网页对象,并使用read()方法获得ur ...

  6. python爬取网站数据保存使用的方法

    这篇文章主要介绍了使用Python从网上爬取特定属性数据保存的方法,其中解决了编码问题和如何使用正则匹配数据的方法,详情看下文     编码问题因为涉及到中文,所以必然地涉及到了编码的问题,这一次借这 ...

  7. python之爬取网页数据总结(一)

    今天尝试使用python,爬取网页数据.因为python是新安装好的,所以要正常运行爬取数据的代码需要提前安装插件.分别为requests    Beautifulsoup4   lxml  三个插件 ...

  8. python爬取网站数据

    开学前接了一个任务,内容是从网上爬取特定属性的数据.正好之前学了python,练练手. 编码问题 因为涉及到中文,所以必然地涉及到了编码的问题,这一次借这个机会算是彻底搞清楚了. 问题要从文字的编码讲 ...

  9. 毕设之Python爬取天气数据及可视化分析

    写在前面的一些P话:(https://jq.qq.com/?_wv=1027&k=RFkfeU8j) 天气预报我们每天都会关注,我们可以根据未来的天气增减衣物.安排出行,每天的气温.风速风向. ...

随机推荐

  1. HTML 005 标题

    <h1>这是一个标题.</h1> <h2>这是一个标题.</h2> <h3>这是一个标题.</h3> HTML 标题 在 HTM ...

  2. 学到了林海峰,武沛齐讲的Day21-完 模块和包

    调用包,会执行包的__init__.py "IF__name__=='__main__':执行当前文件会执行" time random 开始玩高级的了.. 爽

  3. SpringMVC返回类型

    7.SpringMVC的返回值类型和参数传递 1.SpringMVC的返回值类型 (1)ModelAndView返回值类型: 1.1当返回为null时,页面不跳转. 1.2当返回值没有指定视图名时,默 ...

  4. MongoDB 副本集主从切换方法

    一.方法一rs.setpDown() 将Primary节点降级为Secondary节点 myapp:PRIMARY> rs.stepDown() 这个命令会让primary降级为Secondar ...

  5. MyBatis中jdbcType=INTEGER、VARCHAR作用

    Mapper.xml中 pid = #{pid,jdbcType=INTEGER} pid = #{pid} 都可以用 Mybatis中什么时候应该声明jdbcType? 当Mybatis不能自动识别 ...

  6. learning scala PartialFunction

    Partial函数的定义 scala> val isVeryTasty: PartialFunction[String, String] = { case "Glazed Donut& ...

  7. css添加了原始滚动条要隐藏滚动条的显示

    // 添加伪类 <style> ::-webkit-scrollbar {display:none} </style> <div style=" width:2 ...

  8. 【原创】go语言之打印目录

    package main import ( "fmt" "io/ioutil" "log" ) func listFiles(dirname ...

  9. 使用scala通过JNI技术调用c++代码

    scala代码编写 Sample1.scala class Sample1 { // --- Native methods @native def intMethod(n: Int): Int def ...

  10. 1823:【00NOIP提高组】方格取数

    #include<bits/stdc++.h> using namespace std; ][]; ][][][]; inline int max(int x,int y) { retur ...