python爬虫注意点

1.从a标签下获取内容,是string
 #角色
        '''
        <p class="pActor">主演:
        <a target="_blank" href="//dianying.2345.com/list/---ZHANGYI5---.html" title="张译">张译</a>
        <a target="_blank" href="//dianying.2345.com/list/---HUANGJINGYU---.html" title="黄景瑜">黄景瑜</a>
        <a target="_blank" href="//dianying.2345.com/list/---HAIQING---.html" title="海清">海清</a></p>
        '''
        actors=li.find('p',attrs='pActor')
        act=''
        for actor in actors:
             act+=actor.string+' '

2.从<p>标签下获取内容,是text

 #介绍
        '''
        <p class="pTxt pIntroShow">简介:作为远达建筑公司的副总监杨维(王健饰),
        工作上处处受到上级和同事的打压,家庭中妻子(王妍饰)对其也不尊重。各种的压迫下,导致杨维走上歧途。
        将周燕(吕小漫饰)、白亚楠(徐艺涵饰)、沈美玲(刘雨晴饰)分别抓到自己的地窖中,将其虐待......地窖外面
        ,三位女性的亲人苦苦寻找,白亚楠的父亲白景山(梁岩饰)和周燕... 
        <a href="javascript:void(0);" target="_self" class="aMore pIntroShowMore">展开全部 <i class="iconfont"></i></a></p>
        '''
        instroture=li.find('p',attrs={'class':'pTxt pIntroShow'}).text
        print (instroture)
3. python爬虫报错  AttributeError: 'NoneType' object has no attribute 'text',这是
time=li.find('span',attrs={'class':'sIntro'}).text,整个html中没有span 这种类型
这种情况下try  except,说明情况即可:
try:  
    time=li.find('span',attrs={'class':'sIntro'}).text  
    print (time)  
except:  
    print ('还没上映')  
4.python的路径问题
requests.exceptions.MissingSchema: Invalid URL '//imgwx2.2345.com/dypcimg/img/8/65/sup196226_223x310.jpg?1525231260': No schema supplied. Perhaps you meant http:////imgwx2.2345.com/dypcimg/img/8/65/sup196226_223x310.jpg?1525231260?

将路径前加上‘http:’

#下载图片
        with open('C:testdata/image/'+name+'.png','wb+') as f:
            f.write(requests.get('http:'+img_src).content)
5.获取span标签下的a标签下的内容:
 #获取影片的名字
        '''
        <span class="sTit"><a href="//dianying.2345.com/detail/195766.html" target="_blank">妈妈咪鸭</a></span>
        '''
        name=li.find('span',attrs={'class':'sTit'}).a.text
        #print (name)

6.这个问题真的是需要初学者注意,因为没有系统的学习,很多问题只能是自己去碰

首先获取对象的方法是soup.find('标签','属性')

获取的集合方法是soup.find_all('标签','属性')

<ul class="area_three area_list" id="rankList">
		
			<li class="vitem J_li_toggle_date " name="dmvLi">
				
			</li>
		
			<li class="vitem J_li_toggle_date " name="dmvLi">
				
			
			</li>
		
		
	</ul>
当我们获取对象时:
ul=soup.find('ul',attrs={'class':'area_three area_list'})
我们是可以遍历的:
  for li in ul:
        #print (li)
        name=li.find('a',attrs={'class':'mvname'}).text
        print (name)
但是这时就会报错:
   name=li.find('a',attrs={'class':'mvname'}).text
TypeError: find() takes no keyword arguments

但是html中确实是有的!!!

怎样解决呢???   这里就是要提到find_all这个方法了,只有find_all获取的方法,才能遍历从中获取数据!!!!

这样:

 #获取对象
    ul=soup.find('ul',attrs={'class':'area_three area_list'})
    #获取集合
    li_list=ul.find_all('li',attrs={'name':'dmvLi'})

    for li in li_list:
        #print (li)
        name=li.find('a',attrs={'class':'mvname'}).text
        print (name)
这样就能获取到了


7.按属性查询,和直接查询

<div class="score_box">
						
							<h3 class="asc_score">35.83</h3>


							<p class="asc-data clearfix">
								<em class="score-asc"></em>
								<span class="asc-num">+0.658</span>
							</p>
						
						
					</div>
我们这样查询不到!!
time=li.find('h3',attrs={'class':'asc_score'}).text

这有一层一层的属性查询

 sco=li.find('div',attrs={'class':'score_box'}).h3.text






猜你喜欢

转载自blog.csdn.net/qq_38788128/article/details/80481222
今日推荐