HTTPError错误

1、未使用模拟浏览器技术

import re
import urllib.request
import urllib.error

url="http://www.79n.cn/bizhi/zhuomian_101/index_2.html"
url1="http://www.79n.cn/bizhi/zhuomian_101/index_"
url2=url1+str(2)+".html"
print(type(url2))
print(url2)
print(url==url2)
try:
    html1=urllib.request.urlopen(url1).read()
    html1=str(html1)
except urllib.error.HTTPError as e:
    print(e.code)
    print(e.reason)
    print("httperror")
except urllib.error.URLError as e:
    print(e.reason)
    print("urlerror")


pat='<DIV class=wrapperbizhi>.+?<div style="width:300px;height:500px;" id="mouseOverleft">'
res=re.compile(pat).findall(html1)

pat2="http://(.+?).jpg"
res1=re.compile(pat2).findall(res[0])
<class 'str'>
http://www.79n.cn/bizhi/zhuomian_101/index_2.html
True
404
Not Found
httperror
Traceback (most recent call last):
  File "F:/pythonProject/aa.py", line 24, in <module>
    res=re.compile(pat).findall(html1)
NameError: name 'html1' is not defined

2、使用模拟浏览器技术

import urllib.error

url="http://www.79n.cn/bizhi/zhuomian_101/index_2.html"
url1="http://www.79n.cn/bizhi/zhuomian_101/index_"
url2=url1+str(2)+".html"
print(type(url2))
print(url2)
print(url==url2)
try:
    opener=urllib.request.build_opener()
    # 添加opener的请求头(列表形式)
    header=("User-Agent",
              "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36")
    opener.addheaders=[header]
    # 使用opener的open()方法,访问url
    html1=opener.open(url).read()

    html1=str(html1)
except urllib.error.HTTPError as e:
    print(e.code)
    print(e.reason)
    print("httperror")
except urllib.error.URLError as e:
    print(e.reason)
    print("urlerror")


pat='<DIV class=wrapperbizhi>.+?<div style="width:300px;height:500px;" id="mouseOverleft">'
res=re.compile(pat).findall(html1)

pat2="http://(.+?).jpg"
res1=re.compile(pat2).findall(res[0])
print(res1)

<class 'str'>
http://www.79n.cn/bizhi/zhuomian_101/index_2.html
True
['www.79n.cn/uploads/allimg/121102/1-121102094T3']


阅读更多
个人分类: python
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

HTTPError错误

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭