常见状态码

image.png
伪装成浏览器,请求页面,并下载网页

import urllib.request

URL = "https://www.hao123.com/manhua/detail/176"

header ={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '  
                        'Chrome/51.0.2704.63 Safari/537.36'}

opener = urllib.request.build_opener()

opener.add_handler= [header]

data = opener.open(URL).read()

Hfile = open("/Users/vincentwen/Downloads/file.html","wb")
Hfile.write(data)
Hfile.close()



image.png

爬取漫画网站的首页的

import re
import urllib.request
import urllib.error

#读取需要爬取的网址
Readdata= urllib.request.urlopen("http://www.pufei.net/").read()

#对读取的结果进行编码
data = Readdata.decode("utf-8","ignore")

#定义正则表达式,匹配manhua目录下的所有网址
pat= ' href="http://www.pufei.net/manhua/(.*?)/"'

#匹配网页中所有的符合条件的url链接地址
allurl= re.compile(pat).findall(data)

for i in range(0,len(allurl)):
    try:
        print("第"+str(i)+"抓取")
        thisurl= allurl[i]
        file= "/Users/vincentwen/Downloads/"+str(i)+".html"
        urllib.request.urlretrieve(thisurl, file)
        print("------抓取成功-----")

    except urllib.error.URLError as e:
        if hasattr(e, "code"):
            print(e.code)
        if hasattr(e, "reason"):
            print(e.reason)

image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • HTTP状态码大全: http://www.runoob.com/http/http-status-codes.h...
    SinX竟然被占用了阅读 618评论 0 0
  • HTTP状态码(HTTP Status Code) 一些常见的状态码为: 1xx(临时响应)表示临时响应并需要请求...
    默默_David阅读 280评论 0 0
  • HTTP状态码的英文为HTTP Status Code,他是由三个十进制数字组成,第一个十进制数字定义了状态码的类...
    燕妮666_阅读 776评论 0 10
  • http协议状态码分类 本文仅介绍常见的code,更多状态码的详细信息请查看以下链接 https://develo...
    ghbsunny阅读 294评论 0 0
  • 100 Continue 继续,一般在发送post请求时,已发送了http header之后服务端将返回此信息...
    sdcV阅读 155评论 0 0