新手写二手车网站爬虫,爬卖价和原价,原价以<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()
The general idea is to add more selectors, make them empty, and then you make the decision
Give it a try.
If it still doesn’t work, I’ll get the whole paragraph for you and use regex to extract it
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