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']


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值