鱼C论坛

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

求解爬虫

[复制链接]
发表于 2021-1-13 18:54:22 | 显示全部楼层 |阅读模式

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

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

x
  1. import requests,json
  2. from lxml import etree


  3. def gethtml(url):
  4.    
  5.     headers={
  6.     "User-Agent":'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36',
  7.     "Cookie":"",        
  8.     "Referer":"https://pic.sogou.com/pics?query=%E9%A2%86%E5%85%8B",
  9.     }
  10.     r=requests.get(url,headers=headers)     
  11.     if r.status_code !=200:                 
  12.         print("获取url:%s失败!"%(url))
  13.         exit()
  14.     return r                           



  15. def getxpath(r,str):
  16.     html = etree.HTML(r.text)
  17.     reslist = html.xpath(str)
  18.     return reslist



  19. def writetxt(txtpath,data_str):                              
  20.     with open(txtpath, "w") as f:
  21.         f.write(str(data_str))


  22. def writeimg(imgpath,content):        
  23.     with open(imgpath, "wb") as f:
  24.         f.write(content)

  25. def get_sogou_jpg():
  26.     url='https://pic.sogou.com/pics?query=%E9%A2%86%E5%85%8B'
  27.     r=gethtml(url)
  28.     jpg_url_list=getxpath(r,'//img[@id="videoApp"]/div/div[2]/div/ul/li[1]/div/a[1]/img/@src')
  29.     jpg_url=jpg_url_list[0]
  30.     jpg_url="https:"+jpg_url
  31.     r=gethtml(jpg_url)
  32.     writeimg('./搜狗jpg.png',r.content)
  33.     print('恭喜爬到图片')
  34.    
  35. if __name__ == '__main__':

  36.     get_sogou_jpg()
  37.     print('恭喜')

  38.         
复制代码


求解为啥超出范围,
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-1-13 21:22:03 | 显示全部楼层
报错也贴上来
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-1-13 21:31:24 | 显示全部楼层

Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>
======================== RESTART: D:/桌面图标文件目录/妹子图.py ========================
Traceback (most recent call last):
  File "D:/桌面图标文件目录/妹子图.py", line 48, in <module>
    get_mezitu()
  File "D:/桌面图标文件目录/妹子图.py", line 40, in get_mezitu
    mezitu_url=mezitu_url_list[0]
IndexError: list index out of range
>>>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-13 21:35:59 | 显示全部楼层
xxwoaini897 发表于 2021-1-13 21:31
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
Type ...


这个错误这么明显
  1. jpg_url_list=getxpath(r,'//img[@id="videoApp"]/div/div[2]/div/ul/li[1]/div/a[1]/img/@src')
复制代码

上面那行代码匹配出来的列表为空列表,原因如下:
1. 可能是 r 没有内容
2. r 有内容,但是 内容 里没有你要匹配的字符串
3. 匹配规则有误
不过我建议你先学习怎么看报错和调试。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-1-15 11:21:41 | 显示全部楼层
°蓝鲤歌蓝 发表于 2021-1-13 21:35
这个错误这么明显

上面那行代码匹配出来的列表为空列表,原因如下:

非常感谢您,都是断断续续自学了一点,哪里可以看到最新的爬虫教程?就想学会爬虫。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-15 16:02:35 | 显示全部楼层
xxwoaini897 发表于 2021-1-15 11:21
非常感谢您,都是断断续续自学了一点,哪里可以看到最新的爬虫教程?就想学会爬虫。

B站找个日期近的,播放人数较多,评论里说还可以,自己看着也还行的视频就 OK 了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-2 00:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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