|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 fan1993423 于 2018-3-28 10:09 编辑
import urllib.request as ur
import os
def urlopen(url):
req=ur.Request(url)
req.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.90 Safari/537.36 2345Explorer/9.2.1.17116')
response=ur.urlopen(req)
html=response.read()
print(url)
return html
def getpage(url):
html=urlopen(url).decode('utf-8')
a=html.find('current-comment-page')+23
b=html.find(']',a)
return html[a:b]
def findimg(url):
html=urlopen(url).decode('utf-8')
imgadr=[]
a=html.find('img src=')
while a!=-1:
b=html.find('.jpg',a,a+255)
if b!=-1:
imgadr.append(html[a+9:b+4])
else:
b=a+9
a=html.find('img src=',b)
for each in imgadr:
print(each)
def saveimg(folder,img):
pass
def meizi(folder='X',pages=30):
url='http://jandan.net/ooxx/'
pagenum=int(getpage(url))
for i in range(pages):
pagenum-=i
pageurl=url+'page-'+str(pagenum)+'#comments'
img=findimg(pageurl)
saveimg(folder,img)
if __name__=='__main__':
meizi()
这是按小甲鱼老师的视频写的代码,但是出现以下错误,我反复查看了代码,跟小甲鱼没有差别啊,有没有谁能告诉我哪里有问题,急求
http://jandan.net/ooxx/
http://jandan.net/ooxx/page-51#comments
http://jandan.net/ooxx/page-50#comments
http://jandan.net/ooxx/page-48#comments
http://jandan.net/ooxx/page-45#comments
http://jandan.net/ooxx/page-41#comments
http://jandan.net/ooxx/page-36#comments
http://jandan.net/ooxx/page-30#comments
http://jandan.net/ooxx/page-23#comments
http://jandan.net/ooxx/page-15#comments
http://jandan.net/ooxx/page-6#comments
Traceback (most recent call last):
File "C:/Users/zd/Desktop/meizi.py", line 44, in <module>
meizi()
File "C:/Users/zd/Desktop/meizi.py", line 41, in meizi
img=findimg(pageurl)
File "C:/Users/zd/Desktop/meizi.py", line 17, in findimg
html=urlopen(url).decode('utf-8')
File "C:/Users/zd/Desktop/meizi.py", line 6, in urlopen
response=ur.urlopen(req)
File "C:\Python34\lib\urllib\request.py", line 161, in urlopen
return opener.open(url, data, timeout)
File "C:\Python34\lib\urllib\request.py", line 469, in open
response = meth(req, response)
File "C:\Python34\lib\urllib\request.py", line 579, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Python34\lib\urllib\request.py", line 507, in error
return self._call_chain(*args)
File "C:\Python34\lib\urllib\request.py", line 441, in _call_chain
result = func(*args)
File "C:\Python34\lib\urllib\request.py", line 587, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 404: Not Found
小甲鱼的代码你不用看了,早就不能用了
我让你看我这个文章的代码,因为简单的网页结构变了
|
|