求教大神用python爬虫爬网站时报ValueError: invalid literal for int() with base 10
代码如下:
#导入模块
import urllib.request
import os
#打开网址
def url_open(url):
# headers = {‘User-Agent’,‘Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3534.4 Safari/537.36’}
req = urllib.request.Request(url)
req.add_header(‘User-Agent’,‘Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3534.4 Safari/537.36’)
response = urllib.request.urlopen(req)
html = response.read()
return html
#获取页数
def get_page(url):
html = url_open(url).decode(‘utf-8’,‘ignore’)
a = html.find('current-comment-page') + 30
b = html.find(']',a)
return html[a:b]
#找到图片
def find_img(url):
html = url_open(url).decode(‘utf-8’,‘ignore’)
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)
return img_addrs
#保存图片
def save_img(folder,img_addrs):
for each in img_addrs:
filename = each.spilt(’/’)[-1]
with open(filename) as f:
img = url_open(each)
f.write(img)
#下载图片并保存文件夹
def downland_mm(folder=‘Files’,pages = 10):
os.mkdir(folder)
os.chdir(folder)
url = 'http://jandan.net/ooxx/'
page_num = int(get_page(url))
for i in range(pages):
page_num -= i
page_url = url + 'page-' + str(page_num) + '#commemts'
img_addrs = find_img(page_url)
save_img(folder,img_addrs)
if name == ‘main’:
downland_mm()