鱼C论坛

 找回密码
 立即注册
查看: 613|回复: 5

爬虫遇到了问题

[复制链接]
发表于 2018-8-7 15:47:48 | 显示全部楼层 |阅读模式

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

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

x
有没有大神,帮我看看这哪错了,这是爬取头条美图
import requests
import json
import re
import os
from urllib.parse import urlencode
from requests.exceptions import RequestException
from bs4 import BeautifulSoup
from hashlib import md5

def get_page_index(offset,keyword):
    data={
        'offset':offset,
        'format': 'json',
        'keyword': keyword,
        'autoload': 'true',
        'count': 20,
        'cur_tab': 1,
        'from':'search_tab'
    }#字典的形式
    url='https://www.toutiao.com/search_content/?'+urlencode(data)#把字典对象转化为URL请求参数
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
    }
    try:
        response=requests.get(url,headers=headers)
        if response.status_code==200:
            return response.text
        return None
    except RequestException:
        print('请求索引页错误')
        return None

def parse_page_index(html):
    data=json.loads(html)
    if data and 'data' in data.keys():#键值名
        for item in data.get('data'):
            yield item.get('article_url')#提取article_url的信息

def get_page_detail(url):
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
    }
    try:
        response=requests.get(url,headers=headers)
        if response.status_code==200:
            return response.text
        return None
    except RequestException:
        print('请求索引页错误')
        return None

def parse_page_detail(html,url):
    soup=BeautifulSoup(html,'lxml')
    title=soup.select('title')[0].get_text()
    images_pattern=re.compile('gallery: JSON.parse\("(.*?)"\)',re.S)
    result=re.search(images_pattern,html)
    if result:
        data=json.loads(result.group(1).replace('\\',''))
        if data and 'sub_images' in data.keys():
            sub_images=data.get('sub_images')
            images=[item.get('url') for item in sub_images]
            return{
                'title':title,
                'url':url,
                'images':images
            }


def main():
    html=get_page_index(0,'街拍')
    for url in parse_page_index(html):
        html=get_page_detail(url)
        if html:
            result=parse_page_detail(html,url)
            print(result)

if __name__=='__main__':
    main()


错误是   IndexError: list index out of range
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-8-7 15:58:49 | 显示全部楼层
是不是没有爬到?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-8-7 17:25:12 | 显示全部楼层

对,那我该怎么办
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-8-7 17:27:14 | 显示全部楼层
chunguang 发表于 2018-8-7 17:25
对,那我该怎么办

正则表达式用问题

点评

应该是  发表于 2018-8-7 17:27
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-8-7 18:56:33 | 显示全部楼层

不是,我试了一下,老出现这样的问题:
Traceback (most recent call last):
  File "C:/Users/lenovo/Desktop/WeiboList-master/jiebai.py", line 110, in <module>
    main(offset=i*20)
  File "C:/Users/lenovo/Desktop/WeiboList-master/jiebai.py", line 103, in main
    result=parse_page_detail(html,url)
  File "C:/Users/lenovo/Desktop/WeiboList-master/jiebai.py", line 55, in parse_page_detail
    title=soup.select('title')[0].get_text()
IndexError: list index out of range
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-6 05:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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