Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

新手写二手车网站爬虫,爬卖价和原价,原价以<s>删除线形式放在<p class="priType-s">下。但是遇到没有标记原价,也就是并没有<s>标签的情况下,会自动把下一个<s>内的信息写入上一条占位。试了用if len()判断,但是毫无效果。。请问这种情况应当如何解决,把没有<s>标签的情况正确提取出来,用“”或“nodata”显示?

网页源代码如下,
同时包含原价与卖价的:

<p class="priType-s">
 <em class="tag-red">急售</em>
 <em class="tag-yellow">超值</em>
<span>
 <i class="fc-org priType">
                         8.40万
                        </i>
</span>
  <s>17.36万</s>
</p>

没有原价标签的:
<p class="priType-s">

                                                                                      <span>
                <i class="fc-org priType">
                    3.70万
                </i>
              </span>
                                        </p>

代码如下,

import requests
from bs4 import BeautifulSoup

def GetInfo(url):

res=requests.get(url).text
soup=BeautifulSoup(res,'html.parser')
names=soup.select('div.list > ul > li > div > p.infoBox > a')
years=soup.select('div.list > ul > li > div > p.fc-gray')
prices0=soup.select('div.list > ul > li > div > p.priType-s > s')
prices1=soup.select('div.list > ul > li > div > p.priType-s > span > i')
for name,year,price0,price1 in zip(names,years,prices0,prices1):
    data={
        'name':name.get_text(),
        'year':year.get_text().strip().replace('|','').replace(' ',''),
        'price0':price0.get_text(),
        'price1':price1.get_text().strip()
    }
    
    print(data)
return(data)

def Pages():

pageurl='https://www.guazi.com/sh/buy/o{}/'
urls=[pageurl.format(str(i)) for i in range(1,11,1)]
for url in urls:
    GetInfo(url)

Pages()


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
493 views
Welcome To Ask or Share your Answers For Others

1 Answer

大体思路就是 多加选择器,让它为空,然后你做判断


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...