鱼C论坛

 找回密码
 立即注册
查看: 1328|回复: 7

[已解决]弄了个爬虫,但是不会写异常,跪求大神指点!

[复制链接]
发表于 2018-2-9 11:42:16 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
爬小说网站,但是爬一段时间就会出现出现“远程主机没响应”之类的错误,热后又得要重头开始爬,这样的异常该怎么写?求大神指点!
新手,代码写的比较乱,见笑了~!
  1. import requests
  2. import re
  3. import pymysql

  4. conn = pymysql.connect(
  5.     host = '127.0.0.1',
  6.     port = 3306,
  7.     user = 'root',
  8.     passwd = 'root',
  9.     db = 'xiaoshu',
  10.     charset = 'utf8'
  11. )

  12. curson = conn.cursor()

  13. def get_next(url):
  14.     response = requests.get(url)
  15.     response.encoding = 'utf-8'
  16.     return response.text

  17. def re_next(next_html):
  18.     r = re.compile('class="tspage">.*?1/(.*?)&nbsp;每页.*?<a',re.S)
  19.     item = re.findall(r,next_html)
  20.     page_num = item[0]
  21.     return page_num

  22. def re_page_html(page_html):
  23.     r = re.compile('<li>.*?class="s">.*?href="(.*?)"><img.*?">(.*?)</a>.*?class="u">.*?</li>',re.S)
  24.     item = re.findall(r,page_html)
  25.     return item

  26. def get_l(url):
  27.     response = requests.get(url)
  28.     response.encoding = 'utf-8'
  29.     return response.text

  30. def get_arc_list(url):
  31.     response = requests.get(url)
  32.     response.encoding = 'utf-8'
  33.     return response.text

  34. def re_arc_list(arc_list_url):
  35.     r = re.compile('<li><a href="(.*?)">.*?</a></li>',re.S)
  36.     item = re.findall(r,arc_list_url)
  37.     items = item[24:]
  38.     return items

  39. def get_arc_html(uu):
  40.     response = requests.get(uu)
  41.     response.encoding = 'utf-8'
  42.     return response.text

  43. def re_arc(arc_html):
  44.     r = re.compile('class="txt_cont">.*?<h1>(.*?)</h1>.*?html">(.*?)TXT.*?id="content1">(.*?)</div>',re.S)
  45.     item = re.findall(r,arc_html)
  46.     return item

  47. def main():
  48.     for i in range(1,2):
  49.         url = 'http://www.sjtxt.la/soft/{}/Soft_00{}_1.html'.format(i,i)
  50.         next_html = get_next(url)
  51.         page_nums = re_next(next_html)
  52.         for j in range(1,int(page_nums)+1):
  53.             urls = 'http://www.sjtxt.la/soft/{}/Soft_00{}_{}.html'.format(i,i,j)
  54.             page_html = requests.get(urls)
  55.             for data in re_page_html(page_html.text):
  56.                 page_url = 'http://www.sjtxt.la/book/' + data[0][-10:-5]
  57.                 txtname = data[1]
  58.                 print(txtname)
  59.                 curson.execute("insert into book(txtname) value('{}')".format(txtname))
  60.                 idtxtname = curson.lastrowid
  61.                 conn.commit()
  62.                 arc_list_url = get_arc_list(page_url)
  63.                 for u in re_arc_list(arc_list_url):
  64.                     uu = page_url + '/' + u
  65.                     arc_html = get_arc_html(uu)
  66.                     for arc_data in re_arc(arc_html):
  67.                         title = arc_data[0]
  68.                         con = arc_data[2].split()
  69.                         content = ''.join(con)
  70.                         print(title,content)
  71.                         curson.execute("insert into con(idtxtname,title,content) value('{}','{}','{}')".format(idtxtname,title,content))
  72.                         conn.commit()

  73. if __name__ == '__main__':
  74.     main()
复制代码
最佳答案
2018-2-9 12:41:08
注:如果HTTPError 和 URLError 同时使用,HTTPError 必须写在前面。


import urllib.request
from urllib.error import *


写法1:(推荐)
try:
    response = urlopen(req)
except URLError as e:
    if hasattr(e, 'reason'):
        print(e.reason)        
    elif hasattr(e, 'code'):
        print(e.code)
    else:
        print(e.read())


写法2:
req = urllib.request.Request("http://www.fishc.com/ooxx.html")
try:
    urllib.request.urlopen(req)
except HTTPError as e:
    print(e.code)
    print(e.reason)
    print(e.read())
except URLError as e:
    print(e.reason)
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-2-9 11:51:26 | 显示全部楼层
你这个代码看得我眼花缭乱,没有注释,没有报错信息,而且为什么有好几个一毛一样的函数啊?!!
你这个问题应该是服务器发现了你是爬虫所以不和你连接了,因为你代码里面没有加基本的反爬取措施。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-2-9 12:41:08 | 显示全部楼层    本楼为最佳答案   
注:如果HTTPError 和 URLError 同时使用,HTTPError 必须写在前面。


import urllib.request
from urllib.error import *


写法1:(推荐)
try:
    response = urlopen(req)
except URLError as e:
    if hasattr(e, 'reason'):
        print(e.reason)        
    elif hasattr(e, 'code'):
        print(e.code)
    else:
        print(e.read())


写法2:
req = urllib.request.Request("http://www.fishc.com/ooxx.html")
try:
    urllib.request.urlopen(req)
except HTTPError as e:
    print(e.code)
    print(e.reason)
    print(e.read())
except URLError as e:
    print(e.reason)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-2-9 19:23:43 | 显示全部楼层
°蓝鲤歌蓝 发表于 2018-2-9 11:51
你这个代码看得我眼花缭乱,没有注释,没有报错信息,而且为什么有好几个一毛一样的函数啊?!!
你这个问 ...

这个网站就是我朋友的  没有任何反爬措施
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-2-9 19:25:02 | 显示全部楼层
°蓝鲤歌蓝 发表于 2018-2-9 11:51
你这个代码看得我眼花缭乱,没有注释,没有报错信息,而且为什么有好几个一毛一样的函数啊?!!
你这个问 ...

没有一毛一样的函数 只是函数名有点像而已  哈哈哈
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-2-9 19:25:30 | 显示全部楼层
silence181 发表于 2018-2-9 19:23
这个网站就是我朋友的  没有任何反爬措施

那应该是他那边的问题。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-2-9 19:25:35 | 显示全部楼层
ba21 发表于 2018-2-9 12:41
注:如果HTTPError 和 URLError 同时使用,HTTPError 必须写在前面。

谢谢了  我在研究下。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-2-9 19:27:30 | 显示全部楼层
silence181 发表于 2018-2-9 19:25
没有一毛一样的函数 只是函数名有点像而已  哈哈哈

函数名不一样,函数内容都是一样的,应该没必要。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-7 14:13

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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