网页爬虫 - python爬虫用BeautifulSoup爬取<s>元素并写入字典,但某些div下没有这一元素,导致自动写入下一条,如何解决?
高洛峰
高洛峰 2017-04-18 10:32:58
0
3
548

新手写二手车网站爬虫,爬卖价和原价,原价以<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('p.list > ul > li > p > p.infoBox > a')
years=soup.select('p.list > ul > li > p > p.fc-gray')
prices0=soup.select('p.list > ul > li > p > p.priType-s > s')
prices1=soup.select('p.list > ul > li > p > 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()

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(3)
大家讲道理

The general idea is to add more selectors, make them empty, and then you make the decision

大家讲道理
prices0=soup.select('p.list > ul > li > p > p.priType-s > span> i')
prices1=soup.select('p.list > ul > li > p > p.priType-s > span + s')

Give it a try.
If it still doesn’t work, I’ll get the whole paragraph for you and use regex to extract it

Peter_Zhu

Try this idea:
1. Each second-hand car will have a block to display, <p>..</p> and the like
2. In each block, let’s capture the original price and current price. Take
so that the next price point will not be added to the original price of the previous car because a second-hand car does not have the original price

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!