听长风 2021-05-23 17:47 采纳率: 100%
浏览 136
已采纳

复制的python爬虫,在自己电脑上运行会报错,这个怎么解决啊?

python小白,在网上复制的python爬虫代码,在自己电脑上运行会报错。

 感谢大神能看我的问题,希望能帮忙解答一下,谢谢。

代码如下:

import requests
from bs4 import BeautifulSoup
from tqdm import tqdm
import os

#分析网页
def fx(target):
    req = requests.get(url=target)
    req.encoding = 'gb18030'
    html = req.text
    bs = BeautifulSoup(html, 'lxml')
    return bs
#获取模版首页所有分类,创建文件夹
def category(target):
    texts = fx(target).find('div', class_='classify clearfix')
    texts = texts.find_all('a')
    for text in texts:
        url = text.get('href')
        title = text.string
        categoryurl = 'http://pic.netbian.com'+url
        os.mkdir(title) #创建文件夹
        path = './' + title + '/' #分类下载路径
        print(title)
        #print(listurl)
        downpic(categoryurl, path)#下载列表首页
        listpage(categoryurl,path)#下载剩余列表
#获取每个类别所有页面
def listpage(target,path):
    path1 = path
    try:
        texts = fx(target).find('div', class_='page')
        texts = texts.find_all('a')
        #获取每页url
        str1 = texts[-2].string
        listpage = int(str1)
        for i in range(1, listpage):
            listpage1 = str(i+1)
            listurl = target + 'index_' + listpage1 + '.html'
            downpic(listurl,path1) #下载分类中所有页
    except:
        pass
#下载图片
def downpic(target, path):
    path1= path
    texts = fx(target).find('ul', class_='clearfix')
    texts = texts.find_all('a')
    for text in tqdm(texts):
        url = 'http://pic.netbian.com'+ text.get('href')
        title = text.find('b').string
        url1 = 'http://pic.netbian.com' + fx(url).find('a', id='img').find('img').get('src')
        try:
            r = requests.get(url1)
            with open(path1 + title + '.jpg', 'wb') as code:
                code.write(r.content)
        except:
            pass

if __name__== '__main__':
    target = 'http://pic.netbian.com/'
    category(target)

 

  • 写回答

2条回答 默认 最新

  • CSDN专家-HGJ 2021-05-23 18:03
    关注

    代码运行没有问题,检查一下requests版本和bs4版本,可以考虑升级一下。测试通过环境python3.7.6,bs4,'4.9.1',requests,'2.23.0'

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!