鱼C论坛

 找回密码
 立即注册
楼主: 罗同学

[作品展示] 很惭愧、写了一个小爬虫——小草社区~

  [复制链接]
 楼主| 发表于 2018-11-21 10:15:44 | 显示全部楼层
miladuo 发表于 2018-11-21 00:42
执行后,报这个错误。encoding error : input conversion failed due to input error, bytes 0x97 0x00 0x0 ...

可以给个报错的截图嘛、谢谢。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-11-21 11:08:35 | 显示全部楼层
1
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-11-21 11:19:06 | 显示全部楼层
学习学习
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-11-21 11:37:04 | 显示全部楼层
罗同学 发表于 2018-11-21 10:15
可以给个报错的截图嘛、谢谢。

现在在公司运行,报的又不是encoding错误了。报错信息如下:
Traceback (most recent call last):
  File "/Users/abc/Library/Mobile Documents/com~apple~CloudDocs/Python/日常练习/爬虫练习/2.测试中.py", line 65, in <module>
    link.append(urlhead + each.h3.a.get('href'))
AttributeError: 'NoneType' object has no attribute 'a'

Process finished with exit code 1

没看明白这个表示什么意思?each.h3.a  
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-11-21 11:57:34 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-11-21 12:08:35 | 显示全部楼层
看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-11-21 14:05:03 | 显示全部楼层
啥好东西我瞅瞅
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-11-21 14:23:01 | 显示全部楼层
miladuo 发表于 2018-11-21 11:37
现在在公司运行,报的又不是encoding错误了。报错信息如下:
Traceback (most recent call last):
  Fi ...

/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/bin/python3.6 "/Users/abc/Library/Mobile Documents/com~apple~CloudDocs/Python/日常练习/爬虫练习/2.测试中.py"
http://cl.kp9.xyz/read.php?tid=5877
http://cl.kp9.xyz/htm_data/16/1106/524942.html
http://cl.kp9.xyz/htm_data/16/1706/2424348.html
http://cl.kp9.xyz/htm_data/16/1808/344501.html
http://cl.kp9.xyz/htm_data/16/1110/622028.html
http://cl.kp9.xyz/htm_data/16/1707/2519480.html
http://cl.kp9.xyz/htm_data/16/0805/136474.html
http://cl.kp9.xyz/htm_data/16/1109/594741.html
http://cl.kp9.xyz/htm_data/16/1811/3334067.html
http://cl.kp9.xyz/htm_data/16/1811/3335464.html
http://cl.kp9.xyz/htm_data/16/1811/3334354.html
encoding error : input conversion failed due to input error, bytes 0xA6 0x85 0x00 0x00
encoding error : input conversion failed due to input error, bytes 0xA6 0x85 0x00 0x00
encoding error : input conversion failed due to input error, bytes 0x97 0x00 0x00 0x8B
encoding error : input conversion failed due to input error, bytes 0xFF 0x00 0x00 0x48
encoding error : input conversion failed due to input error, bytes 0x93 0x00 0x00 0x3A
encoding error : input conversion failed due to input error, bytes 0x97 0x00 0x00 0x8B
encoding error : input conversion failed due to input error, bytes 0xFF 0x00 0x00 0x48
encoding error : input conversion failed due to input error, bytes 0x93 0x00 0x00 0x3A
encoding error : input conversion failed due to input error, bytes 0xFF 0x00 0x00 0xFE
encoding error : input conversion failed due to input error, bytes 0xA6 0x85 0x00 0x00
encoding error : input conversion failed due to input error, bytes 0x93 0x00 0x00 0x3A
encoding error : input conversion failed due to input error, bytes 0x8F 0x00 0x00 0x20
encoding error : input conversion failed due to input error, bytes 0xFF 0x00 0x00 0xFE
encoding error : input conversion failed due to input error, bytes 0xA6 0x85 0x00 0x00
encoding error : input conversion failed due to input error, bytes 0x93 0x00 0x00 0x3A
encoding error : input conversion failed due to input error, bytes 0x8F 0x00 0x00 0x20
encoding error : input conversion failed due to input error, bytes 0xA9 0x5F 0x00 0x00
encoding error : input conversion failed due to input error, bytes 0xA9 0x5F 0x00 0x00
encoding error : input conversion failed due to input error, bytes 0xFF 0x00 0x00 0x3C
encoding error : input conversion failed due to input error, bytes 0xFF 0x00 0x00 0x3C
encoding error : input conversion failed due to input error, bytes 0x9C 0x00 0x00 0x29
encoding error : input conversion failed due to input error, bytes 0x9C 0x00 0x00 0x29
Done

现在报错成这样了。还是encoding的错误。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-11-21 16:36:21 | 显示全部楼层
miladuo 发表于 2018-11-21 11:37
现在在公司运行,报的又不是encoding错误了。报错信息如下:
Traceback (most recent call last):
  Fi ...

each.h3.a  这个是提取标签、看下网页的代码可以发现页面中子网页的地址全都是在each.h3.a  标签下的。
至于那个encoding的报错、emmmm,我去发个帖子求助去。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-11-24 12:36:13 | 显示全部楼层
miladuo 发表于 2018-11-21 14:23
/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/bin/python3.6 "/Users/abc/ ...

emmm、今天休息又试了一下、好用的啊、没有报错额。
注意49、50行的地址前面现在是“http://cl.kp9.xyz”,把代码里面改一下
QQ图片20181124123306.png
QQ图片20181124123557.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-11-26 10:21:47 | 显示全部楼层
罗同学 发表于 2018-11-24 12:36
emmm、今天休息又试了一下、好用的啊、没有报错额。
注意49、50行的地址前面现在是“http://cl.kp9.xyz ...

你运行我的看看。麻烦了。我修改了存储的路径和URL链接。


import requests
from bs4 import BeautifulSoup as bs
import re
import os
import socket
import time
import threading


def url_open(url):
    socket.setdefaulttimeout(20)
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36 LBBROWSER'}
    res = requests.get(url, headers=headers)
    return res


def save(url):  # 传入每个子网页链接
    res = url_open(url)
    res.encoding = 'gbk'
    soup = bs(res.text, 'lxml')
    title = soup.find('title').text.split('-')[0]  # 标题
    # os.mkdir(title)
    # os.chdir(title)
    temp = soup.find_all('tr', class_='tr3')
    img = re.findall(r'data-src="(.*?jpg)" type', str(temp))


    imglist = []
    for each in img:
        imglist.append(each)
    for each in imglist:
        filename = each.split('/')[-1]
        img = url_open(each)
        print('saving...+%s' % filename)

        with open(title + filename, 'wb')as f:
            f.write(img.content)
            # os.chdir('..')


if __name__ == '__main__':
    os.makedirs(r'/Users/abc/spider/training/1024', exist_ok=True)
    os.chdir(r'/Users/abc/spider/training/1024')
    url = 'http://cl.kp9.xyz/thread0806.php?fid=16&search=&page=1'  # 默认爬取第一个页面,毕竟要注意身体,需要多个页面的话,自己加个for循环也不是什么难事~
    urlhead = 'http://cl.kp9.xyz/'  # 页面解析出来的连接前面需要加上这个头才能打开,根据多年经验这个头是会变的,如果哪天不能用了自己看下是不是这个头变了
    res = url_open(url)
    res.encoding = 'gbk'

    '''找到页面中的所有子网页'''
    soup = bs(res.text, 'lxml')
    temp = soup.find_all('td', class_="tal")
    link = []
    for each in temp:
        urls1 = urlhead + each.h3.a.get('href')
        link.append(urls1)
    # del link[0:10]

    downloads = []
    for each in link:
        down = threading.Thread(target=save, args=[each])
        downloads.append(down)
        down.start()

    for each in downloads:
        each.join()
    print('Done')

运行结果:
encoding error : input conversion failed due to input error, bytes 0xFF 0x00 0x00 0x48
encoding error : input conversion failed due to input error, bytes 0xA6 0x85 0x00 0x00
encoding error : input conversion failed due to input error, bytes 0xA9 0x5F 0x00 0x00
encoding error : input conversion failed due to input error, bytes 0xFF 0x00 0x00 0x48
encoding error : input conversion failed due to input error, bytes 0xFF 0x00 0x00 0x3C
encoding error : input conversion failed due to input error, bytes 0x9C 0x00 0x00 0x29
encoding error : input conversion failed due to input error, bytes 0x8F 0x00 0x00 0x20
encoding error : input conversion failed due to input error, bytes 0x97 0x00 0x00 0x8B
encoding error : input conversion failed due to input error, bytes 0xFF 0x00 0x00 0xFE
encoding error : input conversion failed due to input error, bytes 0xA6 0x85 0x00 0x00
encoding error : input conversion failed due to input error, bytes 0xA9 0x5F 0x00 0x00
encoding error : input conversion failed due to input error, bytes 0x8F 0x00 0x00 0x3C
encoding error : input conversion failed due to input error, bytes 0xA6 0x85 0x00 0x00
encoding error : input conversion failed due to input error, bytes 0x9C 0x00 0x00 0x29
encoding error : input conversion failed due to input error, bytes 0xFF 0x00 0x00 0xFE
encoding error : input conversion failed due to input error, bytes 0xA9 0x5F 0x00 0x00
encoding error : input conversion failed due to input error, bytes 0x8F 0x00 0x00 0x3C
encoding error : input conversion failed due to input error, bytes 0x97 0x00 0x00 0x8B
encoding error : input conversion failed due to input error, bytes 0xFF 0x00 0x00 0x3C
encoding error : input conversion failed due to input error, bytes 0xA6 0x85 0x00 0x00
encoding error : input conversion failed due to input error, bytes 0xA6 0x85 0x00 0x00
encoding error : input conversion failed due to input error, bytes 0xA6 0x85 0x00 0x00
encoding error : input conversion failed due to input error, bytes 0xA9 0x5F 0x00 0x00
encoding error : input conversion failed due to input error, bytes 0x8F 0x00 0x00 0x20
Done

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-11-26 10:59:43 | 显示全部楼层
miladuo 发表于 2018-11-26 10:21
你运行我的看看。麻烦了。我修改了存储的路径和URL链接。

emmm、修改一下url_open函数,如果还不行、我就也不会了
  1. def url_open(url):
  2.     socket.setdefaulttimeout(20)
  3.     headers = {
  4.         'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36 LBBROWSER'}
  5.     res = requests.get(url, headers=headers).decode('gbk', 'replace').encode('gbk')
  6.     return res
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-11-26 11:25:21 | 显示全部楼层
学习一下
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-11-26 13:34:08 | 显示全部楼层
以学习的名义,哈哈
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-11-26 15:40:51 | 显示全部楼层
学习
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-11-26 15:43:35 | 显示全部楼层
  学习啦
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-11-26 16:08:44 | 显示全部楼层
1024
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-11-26 17:47:34 | 显示全部楼层
脸红是怎么样的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-11-26 20:51:45 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-11-27 00:01:14 | 显示全部楼层
是什么没看懂啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-5 17:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表