鱼C论坛

 找回密码
 立即注册
查看: 2007|回复: 2

[已解决]一段抓取知乎妹子的爬虫代码不能正常运行

[复制链接]
发表于 2016-9-17 10:42:54 | 显示全部楼层 |阅读模式

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

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

x
一段抓取知乎妹子的爬虫代码不能正常运行,总是出现问题说缩进有问题,但是我找不出来在哪里,请大神帮忙找找并修改!谢谢!
问题:运行时总显示unexpected indet
源码如下:
#encoding:utf-8
import urllib.request
import urllib
import re
import os
from bs4 import BeautifulSoup

url='https://www.zhihu.com/question/40753170'
urlop=urllib.request.urlopen(url)
data=urlop.read().decode('utf-8')
bs=BeautifulSoup(data)

def gettitle(url):
    title=bs.find('span',{"class":"zm-editable-content"})#找到标题
    title=title.get_text()
return(title)

def getpicurl(url):#得到图片链接
    pics=re.compile('img.+?src=\"(https.+?)\"')
#pics=re.compile('img.+?src=(https.+?)')
    return(pics)

def downpics():
    title=gettitle(url)
print(title)
    dirpath='f:/知乎/'+title+"/"
    if not os.path.exists(dirpath):
        os.makedirs(dirpath)#生成了问题标题相应的文件夹
    pics=getpicurl(url)
    a=1
    urls=[]
for x in pics.findall(data):#去除重复的图片链接
        if x not in urls:
            urls.append(x)
for x in urls:
try:
                imgdata=urllib.request.urlopen(x).read()
                b = (x.rfind("."))
                imgpath=str(dirpath)+str(a)+x[b:]
print(imgpath)
print(x)
                a+=1
                file=open(imgpath,'wb')
                file.write(imgdata)
                file.flush()
                file.close()
except:
continue
downpics();

最佳答案
2016-9-17 11:28:18
本帖最后由 无符号整形 于 2016-9-17 11:33 编辑
  1. def gettitle(url):
  2.      title=bs.find('span',{"class":"zm-editable-content"})#找到标题
  3.      title=title.get_text()
  4. return(title)
复制代码

=>
  1. def gettitle(url):
  2.      title=bs.find('span',{"class":"zm-editable-content"})#找到标题
  3.      title=title.get_text()
  4.      return(title)
复制代码





  1. for x in urls:
  2. try:
  3.                  imgdata=urllib.request.urlopen(x).read()
  4.                  b = (x.rfind("."))
  5.                  imgpath=str(dirpath)+str(a)+x[b:]
  6. print(imgpath)
  7. print(x)
  8.                  a+=1
  9.                  file=open(imgpath,'wb')
  10.                  file.write(imgdata)
  11.                  file.flush()
  12.                  file.close()
  13. except:
  14. continue
复制代码

=>
  1. for x in urls:
  2.     try:
  3.         imgdata=urllib.request.urlopen(x).read()
  4.         b = (x.rfind("."))
  5.         imgpath=str(dirpath)+str(a)+x[b:]
  6.         print(imgpath)
  7.         print(x)
  8.         a+=1
  9.         file=open(imgpath,'wb')
  10.         file.write(imgdata)
  11.         file.flush()
  12.         file.close()
  13.     except:
  14.         continue
复制代码




  1. def downpics():
  2.      title=gettitle(url)
  3. print(title)
  4.      dirpath='f:/知乎/'+title+"/"
  5.      if not os.path.exists(dirpath):
  6.          os.makedirs(dirpath)#生成了问题标题相应的文件夹
  7.      pics=getpicurl(url)
  8.      a=1
  9.      urls=[]
复制代码

=>
  1. def downpics():
  2.      title=gettitle(url)
  3.      print(title)
  4.      dirpath='f:/知乎/'+title+"/"
  5.      if not os.path.exists(dirpath):
  6.          os.makedirs(dirpath)#生成了问题标题相应的文件夹
  7.      pics=getpicurl(url)
  8.      a=1
  9.      urls=[]
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-9-17 11:28:18 | 显示全部楼层    本楼为最佳答案   
本帖最后由 无符号整形 于 2016-9-17 11:33 编辑
  1. def gettitle(url):
  2.      title=bs.find('span',{"class":"zm-editable-content"})#找到标题
  3.      title=title.get_text()
  4. return(title)
复制代码

=>
  1. def gettitle(url):
  2.      title=bs.find('span',{"class":"zm-editable-content"})#找到标题
  3.      title=title.get_text()
  4.      return(title)
复制代码





  1. for x in urls:
  2. try:
  3.                  imgdata=urllib.request.urlopen(x).read()
  4.                  b = (x.rfind("."))
  5.                  imgpath=str(dirpath)+str(a)+x[b:]
  6. print(imgpath)
  7. print(x)
  8.                  a+=1
  9.                  file=open(imgpath,'wb')
  10.                  file.write(imgdata)
  11.                  file.flush()
  12.                  file.close()
  13. except:
  14. continue
复制代码

=>
  1. for x in urls:
  2.     try:
  3.         imgdata=urllib.request.urlopen(x).read()
  4.         b = (x.rfind("."))
  5.         imgpath=str(dirpath)+str(a)+x[b:]
  6.         print(imgpath)
  7.         print(x)
  8.         a+=1
  9.         file=open(imgpath,'wb')
  10.         file.write(imgdata)
  11.         file.flush()
  12.         file.close()
  13.     except:
  14.         continue
复制代码




  1. def downpics():
  2.      title=gettitle(url)
  3. print(title)
  4.      dirpath='f:/知乎/'+title+"/"
  5.      if not os.path.exists(dirpath):
  6.          os.makedirs(dirpath)#生成了问题标题相应的文件夹
  7.      pics=getpicurl(url)
  8.      a=1
  9.      urls=[]
复制代码

=>
  1. def downpics():
  2.      title=gettitle(url)
  3.      print(title)
  4.      dirpath='f:/知乎/'+title+"/"
  5.      if not os.path.exists(dirpath):
  6.          os.makedirs(dirpath)#生成了问题标题相应的文件夹
  7.      pics=getpicurl(url)
  8.      a=1
  9.      urls=[]
复制代码

评分

参与人数 1鱼币 +5 贡献 +5 收起 理由
SixPy + 5 + 5 支持!

查看全部评分

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

使用道具 举报

 楼主| 发表于 2016-9-17 13:07:19 | 显示全部楼层
首先谢谢@无符号整形好吧,我修改了试试,还是没成功!我依然不知道哪里错了,简直了,看来还要学啊,赶紧走出新手村才行~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-19 11:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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