|
10鱼币
代码按甲鱼老师的写法写的,但是报错:
代码:
import urllib.request
import os
def url_open(url):
heads = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}
req = urllib.request.Request(url,headers = heads)
response = urllib.request.urlopen(req)
html = response.read()
return html
def get_page(url):
html = url_open(url).decode('utf-8')
a = html.find('current-comment-page')+23
b = html.find(']',a)
return html[a:b]
def find_imgs(url):
html = url_open(url).decode('utf-8')
img_addrs = []
a = html.find('img src=')
while a!= -1:
b = html.find('.jpg',a,a+255)
if b != -1:
img_addrs.append(html[a+9:b+4])
else:
b = a+9
a = html.find('img src=',b)
for each in img_addrs:
print(each)
def save_imgs(folder,img_adddrs):
pass
def download_mm(folder = '妹子图片',pages= 15):
os.mkdir(folder)
os.chdir(folder)
url = 'http://jandan.net/ooxx/'
page_number = int(get_page(url))
for i in range(pages):
page_number = page_number - i
page_url = url + 'page-' + str(page_number) +'#comments'
img_addrs = find_imgs(page_url)
#save_imgs(folder,img_adddrs)
if __name__ == '__main__':
download_mm()
报错原因:
Traceback (most recent call last):
File "C:\Users\songyuxue\Desktop\download_mmtp.py", line 61, in <module>
download_mm()
File "C:\Users\songyuxue\Desktop\download_mmtp.py", line 56, in download_mm
img_addrs = find_imgs(page_url)
File "C:\Users\songyuxue\Desktop\download_mmtp.py", line 23, in find_imgs
html = url_open(url).decode('utf-8')
File "C:\Users\songyuxue\Desktop\download_mmtp.py", line 8, in url_open
response = urllib.request.urlopen(req)
File "C:\Users\songyuxue\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 162, in urlopen
return opener.open(url, data, timeout)
File "C:\Users\songyuxue\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 471, in open
response = meth(req, response)
File "C:\Users\songyuxue\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 581, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Users\songyuxue\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 509, in error
return self._call_chain(*args)
File "C:\Users\songyuxue\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 443, in _call_chain
result = func(*args)
File "C:\Users\songyuxue\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 589, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 404: Not Found
刚刚一直发布出来东西,,,js就是javascript动态加载,具体我也不大清楚,最近才在看,
你要爬这种可以看看小甲鱼的极客python,那里面讲了一点。还可以试试seleninum这种模拟浏览器行为的库。
我也很菜哈哈知道的就这么多了。
另外学爬虫的话给你分享一个课程
https://www.bilibili.com/video/av18202461
这里面讲的还可以,把前面的安装什么东西的跳过就行了。
慢慢来哈哈,一开始先从简单的网站爬起,在学习中接触更高级的
|
最佳答案
查看完整内容
刚刚一直发布出来东西,,,js就是javascript动态加载,具体我也不大清楚,最近才在看,
你要爬这种可以看看小甲鱼的极客python,那里面讲了一点。还可以试试seleninum这种模拟浏览器行为的库。
我也很菜哈哈知道的就这么多了。
另外学爬虫的话给你分享一个课程
https://www.bilibili.com/video/av18202461
这里面讲的还可以,把前面的安装什么东西的跳过就行了。
慢慢来哈哈,一开始先从简单的网站爬起,在学习中接触更高级 ...
|