鱼C论坛

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

爬虫遇到了问题

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

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

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

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

  9. def get_page_index(offset,keyword):
  10.     data={
  11.         'offset':offset,
  12.         'format': 'json',
  13.         'keyword': keyword,
  14.         'autoload': 'true',
  15.         'count': 20,
  16.         'cur_tab': 1,
  17.         'from':'search_tab'
  18.     }#字典的形式
  19.     url='https://www.toutiao.com/search_content/?'+urlencode(data)#把字典对象转化为URL请求参数
  20.     headers = {
  21.         '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'
  22.     }
  23.     try:
  24.         response=requests.get(url,headers=headers)
  25.         if response.status_code==200:
  26.             return response.text
  27.         return None
  28.     except RequestException:
  29.         print('请求索引页错误')
  30.         return None

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

  36. def get_page_detail(url):
  37.     headers = {
  38.         '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'
  39.     }
  40.     try:
  41.         response=requests.get(url,headers=headers)
  42.         if response.status_code==200:
  43.             return response.text
  44.         return None
  45.     except RequestException:
  46.         print('请求索引页错误')
  47.         return None

  48. def parse_page_detail(html,url):
  49.     soup=BeautifulSoup(html,'lxml')
  50.     title=soup.select('title')[0].get_text()
  51.     images_pattern=re.compile('gallery: JSON.parse\("(.*?)"\)',re.S)
  52.     result=re.search(images_pattern,html)
  53.     if result:
  54.         data=json.loads(result.group(1).replace('\\',''))
  55.         if data and 'sub_images' in data.keys():
  56.             sub_images=data.get('sub_images')
  57.             images=[item.get('url') for item in sub_images]
  58.             return{
  59.                 'title':title,
  60.                 'url':url,
  61.                 'images':images
  62.             }


  63. def main():
  64.     html=get_page_index(0,'街拍')
  65.     for url in parse_page_index(html):
  66.         html=get_page_detail(url)
  67.         if html:
  68.             result=parse_page_detail(html,url)
  69.             print(result)

  70. if __name__=='__main__':
  71.     main()

复制代码



错误是   IndexError: list index out of range
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-8-7 15:58:49 | 显示全部楼层
是不是没有爬到?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

对,那我该怎么办
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

正则表达式用问题

点评

应该是  发表于 2018-8-7 17:27
小甲鱼最新课程 -> https://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
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-7 11:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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