python爬虫实例代码和分析,Python爬虫实战requests模块Python实现爬取网站漫画
python爬虫实例代码和分析,Python爬虫实战requests模块Python实现爬取网站漫画找到图片链接后,接着就要想办法从该数据包中获取,也就是访问该数据包的链接,从数据包中提取图片链接。通过多页的数据包,观察以下数据包链接,发现 chapter_newid 每次翻页会发生变化的, comic_id 是一本漫画的唯一标识。漫画其实是一张一张图片来着,所以我们先找到这些图片的链接在哪里!因为本文是为了实现想看什么漫画就爬取什么漫画,所以搜索任一漫画,这里以神印王座为例,然后点进去进入详情页查看任一话;在浏览页中,网页源代码是没有我们需要的数据,所以需要打开开发者工具进行抓包,最终成功找到图片的链接。shutil模块;以及一些Python自带的模块。安装Python并添加到环境变量,pip安装需要的相关模块即可。
前言今天带大家爬取爬取网站漫画,废话不多说,直接开始~
开发工具Python版本:3.6.4
相关模块:requests模块;
re模块;
shutil模块;
以及一些Python自带的模块。
环境搭建安装Python并添加到环境变量,pip安装需要的相关模块即可。
思路分析漫画其实是一张一张图片来着,所以我们先找到这些图片的链接在哪里!因为本文是为了实现想看什么漫画就爬取什么漫画,所以搜索任一漫画,这里以神印王座为例,然后点进去进入详情页查看任一话;在浏览页中,网页源代码是没有我们需要的数据,所以需要打开开发者工具进行抓包,最终成功找到图片的链接。
找到图片链接后,接着就要想办法从该数据包中获取,也就是访问该数据包的链接,从数据包中提取图片链接。通过多页的数据包,观察以下数据包链接,发现 chapter_newid 每次翻页会发生变化的, comic_id 是一本漫画的唯一标识。
kanman/api/getchapterinfov2?product_id=1&productname=kmh&platformname=pc&comic_id=5323&chapter_newid=1006&isWebp=1&quality=middle\
kanman/api/getchapterinfov2?product_id=1&productname=kmh&platformname=pc&comic_id=5323&chapter_newid=2003&isWebp=1&quality=middle\
kanman/api/getchapterinfov2?product_id=1&productname=kmh&platformname=pc&comic_id=5323&chapter_newid=3004&isWebp=1&quality=middle
接着查找这两个参数是从哪里来的。进入首页搜索神印王座,接着查看网页源代码,发现可以在网页源代码中找到进入漫画详情页的url;我试着用正则表达式和xpath语法进行提取时,发现困难重重,源代码中的HTML标签有很多的都相同的,且发现源代码中不止一本漫画。
接着我试着搜索其它漫画,发现源代码中没有,我才发现我掉坑里,后来发现该源代码是网站首页的源代码,大意了,泪目!但没关系,源代码中没有,我们去抓包。
打开开发者工具,进入Network中的XHR,搜索神印王座,第一次搜索的时候抓到一条数据包,不过他报红了:
但里面是有我们需要的内容的。不过因为报红,我们在开发者工具中是无法看到数据的,得点开数据包:
如果需要获取不报红的数据包,需要重新点击一下输入框,他就会加载出来了,如果只刷新网页和重新点击搜索他都是无法获取到的。
拿到数据包后,我们找到漫画的唯一标识 comic_id ,只需要该数据包中提取出来:
找到 comic_id 后,接着找 chapter_newid 。 chapter_newid 变化规律每本漫画他都是不同的;但如果你第一次搜索的是斗罗大陆,你会发现, chapter_newid 他是递增式变化的。
那 chapter_newid 怎么找呢,进入到漫画的详情页,前面我们知道神印王座的第一话的 chapter_newid 是 1006 ,那我们直接在开发者工具中搜索1006,最终在详情页源代码中找到:
那么我们知道,首个 chapter_newid 是详情页静态加载来的,可以在详情页的源代码中提取出来,而该网址是 kanman/ comic_id 构成的:
这里只要第一话的 chapter_newid ,那其它的从哪里得到呢?经过我的查找,发现后一页的 chapter_newid 是在前一页中获取到的:
构建提取 comic_id 和 chapter_id 函数:
def get_comic(url):\
data = get_response(url).json()['data']\
for i in data:\
comic_id = i['comic_id']\
chapter_newid_url = f'kanman/{comic_id}/'\
chapter_newid_html = get_response(chapter_newid_url).text\
chapter_id = re.findall('{"chapter_id":"(.*?)"}' chapter_newid_html)\
data_html(comic_id chapter_id[0])
关键代码,如果以前爬取过微博评论数据的,就会发现,二者的套路差不多,翻页的数值都需要从前一页中获取:
def data_html(comic_id chapter_id):\
try:\
a = 1\
while True: # 循环获取chapter_id\
if a == 1:\
comic_url = f'kanman/api/getchapterinfov2?product_id=1&productname=kmh&platformname=pc&comic_id={comic_id}&chapter_newid={chapter_id}&isWebp=1&quality=middle'\
else:\
comic_url = f'kanman/api/getchapterinfov2?product_id=1&productname=kmh&platformname=pc&comic_id={comic_id}&chapter_newid={chapter_newid}&isWebp=1&quality=middle'\
comic_htmls = get_response(comic_url).text\
comic_html_jsons = json.loads(comic_htmls)\
if a == 1:\
chapter_newid = jsonpath.jsonpath(comic_html_jsons '$..chapter_newid')[1]\
else: # 自第二条url开始,提取规则 1\
chapter_newid = jsonpath.jsonpath(comic_html_jsons '$..chapter_newid')[2]\
current_chapter = jsonpath.jsonpath(comic_html_jsons '$..current_chapter')\
for img_and_name in current_chapter:\
image_url = jsonpath.jsonpath(img_and_name '$..chapter_img_list')[0] # 图片url\
# chapter_name 中存在空格,所以需要用strip去除\
chapter_name = jsonpath.jsonpath(img_and_name '$..chapter_name')[0].strip()\
save(image_url chapter_name)\
a = 1\
except IndexError:\
pass
保存数据:
def save(image_url chapter_name):\
for link_url in image_url: # 图片名称\
image_name = ''.join(re.findall('/(\d .jpg)-kmh' str(link_url)))\
image_path = data_path chapter_name\
if not os.path.exists(image_path): # 创建章节标题文件夹\
os.mkdir(image_path)\
image_content = get_response(link_url).content\
filename = '{}/{}'.format(image_path image_name)\
with open(filename mode='wb') as f:\
f.write(image_content)\
print(image_name)\
get_img(chapter_name) # 拼接函数章节标题,非必需
控制台:
if __name__ == '__main__':\
key = input('请输入你要下载的漫画:')\
data_path = r'D:/数据小刀/爬虫④/漫画/{}/'.format(key)\
if not os.path.exists(data_path): # 根据用户输入的漫画名称创建文件夹\
os.mkdir(data_path) \
url = f'kanman/api/getsortlist/?search_key={key}' # 该url由去除不必要的参数得到\
get_comic(url)
保存的数据展示