python爬虫(八、爬取图片社的小姐姐图片并下载)报错, import urllib.request,parser ,ImportError: No module named reques

1 篇文章 0 订阅
1 篇文章 0 订阅

目录:
一. 学习感言
二. 遇到的报错
三. 参考链接
四. 原文链接
五. 参考代码,python2.7
六. 原文代码,python3.x

一. 学习感言。学习爬虫N天,感觉天下的坑都不是一般的深。要习惯不断跳入坑里,再迅速弹出,才能习惯程序工作。学习爬虫N天,以为拿来的code可以直接使用,结果都是要debug一下才能使用。debug成功后回顾,才知道博主已经很不容易,像同事所说,只需要做几个小的修改就可以成功。可惜很多人都小的修改吓破了胆,连再尝试一下的勇气都没有了。
二。遇到的报错。言归正传,
在使用爬虫链接,https://blog.csdn.net/jziwjxjd/article/details/106864267,
python爬虫(八、爬取图片社的小姐姐图片并下载)的时候,陆续遇到3个报错,
import urllib.request,parser ,ImportError: No module named reques。
回头一看,这其中说的是2件事情,2个报错。
搜索,debug了一阵之后,发现是版本不同导致,我用的是python2.7, 作者用的python3.5.
把urllib.request 改为urllib2, 把parser 改为urlparser 就可以pass 了。
python2.7, urllib2, urlparser
python3.5. urllib.request , url.parser
修改之后的code, python爬虫(八、爬取图片社的小姐姐图片并下载)
三 参考链接
搜索,debug参考链接
#https://blog.csdn.net/qq_34802511/article/details/90754707
#https://blog.csdn.net/echojosedream/article/details/52938136
#https://blog.csdn.net/testcs_dn/article/details/55807101
#0503,0826.
#https://blog.csdn.net/u012720990/article/details/84952602 , urllib2 在 python3.x 中被改为urllib.request
https://blog.csdn.net/testcs_dn/article/details/55807101, urlparse模块在Python 3中重命名为urllib.parse

四。原文链接
pachong原文链接:
https://blog.csdn.net/jziwjxjd/article/details/106864267

五。参考代码,python2.7, pycharm2019.3
注意手工建目录 “D:\妹子图片”, 否则IOError: [Errno 2] No such file or directory:

#!/usr/bin/env python
#-*- coding:utf-8 -*-
# author:StevenC     
# datetime:2021/5/2 23:34
# software: PyCharm
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
""" 
import re
import xlwt
import urllib2                             #urllib.request
import urlparse
from bs4 import BeautifulSoup

def ask(url):
    head = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36"}
    re = urllib2.Request(url=url,headers=head)
    res = urllib2.urlopen(re)
    html = res.read().decode('utf-8')
    return html

def download_img(baseurl):
    html = ask(baseurl)
    soup = BeautifulSoup(html,'html.parser')
    k=0
    for item in soup.find_all('div',class_='list'):
        item = str(item)
        tupian = re.findall(findload,item)[0]
        url = "http:"+tupian
        name = "D:\\妹子图片\\"
        name = name + str(k) + ".jpg"
        k+=1
        img = urllib2.urlopen(url)
        f = open(name, 'wb')
        f.write(img.read())
        f.close

findload = re.compile('<img.*data-original="(.*?)"')
url = "http://699pic.com/tupian/xiaojiejie.html"
download_img(url)

六。原文代码(python3.x):
https://blog.csdn.net/jziwjxjd/article/details/106864267

import re
import xlwt
import urllib.request,parser
from bs4 import BeautifulSoup


def ask(url):
    head = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36"}
    re = urllib.request.Request(url=url,headers=head)
    res = urllib.request.urlopen(re)
    html = res.read().decode('utf-8')
    return html

def download_img(baseurl):
    html = ask(baseurl)
    soup = BeautifulSoup(html,'html.parser')
    k=0
    for item in soup.find_all('div',class_='list'):
        item = str(item)
        tupian = re.findall(findload,item)[0]
        url = "http:"+tupian
        name = "D:\\妹子图片\\"
        name = name + str(k) + ".jpg"
        k+=1
        img = urllib.request.urlopen(url)
        f = open(name, 'wb')
        f.write(img.read())
        f.close


findload = re.compile('<img.*data-original="(.*?)"')
url = "http://699pic.com/tupian/xiaojiejie.html"
download_img(url)




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值