python爬取文件时,内容为空

时间:2019-08-23
本文章向大家介绍python爬取文件时,内容为空,主要包括python爬取文件时,内容为空使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

解决方式:

img_res = requests.get(src,headers=header)
在header中加上referer防盗链
加上防盗链header的例子:
    header = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64; rv:66.0) Gecko/20100101 Firefox/66.0",
"Referer":"https://www.mzitu.com/",}






还有关于header的问题,
如果进不去网址,说明浏览器防火墙给阻断了,这时候要加入 虚拟浏览器头:例子如上
     header = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64; rv:66.0) Gecko/20100101 Firefox/66.0",
"Referer":"https://www.mzitu.com/",}





还有关于python进行一段时间便出现'NoneType' object has no attribute 'find'
这是空类型的问题,即 <class 'NoneType'>

   用isinstance()函数将空类型过滤掉。

         例子:

                 源代码如下:

max_no = soup_item.find('div', class_='pagenavi').find_all('span')[6].get_text()

            解决方式:

max_no = soup_item.find('div', class_='pagenavi').find_all('span')[6].get_text()
     if isinstance(max_no,bs4.element.Tag):
            #后边的代码

  

  ************************************************************************************************************

原文地址:https://www.cnblogs.com/xww115/p/11398635.html