鱼C论坛

 找回密码
 立即注册
查看: 1083|回复: 1

[已解决]帮优化下代码【爬虫,瓜子二手车,多次判断】

[复制链接]
发表于 2020-9-20 15:40:49 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
import requests
from lxml import etree
headers={
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36',
        'Cookie': 'antipas=38p542Z93260M92A5S55D; uuid=2bc4d66a-282f-4d2e-8202-3600830a5c79; clueSourceCode=%2A%2300; user_city_id=45; Hm_lvt_936a6d5df3f3d309bda39e92da3dd52f=1600584882; ganji_uuid=5704297242784487577783; sessionid=b8d3e268-ef08-47fb-eef2-98bb4bf0ba54; lg=1; lng_lat=104.078574_30.481192; gps_type=1; close_finance_popup=2020-09-20; cainfo=%7B%22ca_a%22%3A%22-%22%2C%22ca_b%22%3A%22-%22%2C%22ca_s%22%3A%22seo_baidu%22%2C%22ca_n%22%3A%22default%22%2C%22ca_medium%22%3A%22-%22%2C%22ca_term%22%3A%22-%22%2C%22ca_content%22%3A%22-%22%2C%22ca_campaign%22%3A%22-%22%2C%22ca_kw%22%3A%22-%22%2C%22ca_i%22%3A%22-%22%2C%22scode%22%3A%22-%22%2C%22keyword%22%3A%22-%22%2C%22ca_keywordid%22%3A%22-%22%2C%22display_finance_flag%22%3A%22-%22%2C%22platform%22%3A%221%22%2C%22version%22%3A1%2C%22client_ab%22%3A%22-%22%2C%22guid%22%3A%222bc4d66a-282f-4d2e-8202-3600830a5c79%22%2C%22ca_city%22%3A%22cd%22%2C%22sessionid%22%3A%22b8d3e268-ef08-47fb-eef2-98bb4bf0ba54%22%7D; preTime=%7B%22last%22%3A1600584999%2C%22this%22%3A1600584880%2C%22pre%22%3A1600584880%7D; cityDomain=linfen; _gl_tracker=%7B%22ca_source%22%3A%22-%22%2C%22ca_name%22%3A%22-%22%2C%22ca_kw%22%3A%22-%22%2C%22ca_id%22%3A%22-%22%2C%22ca_s%22%3A%22self%22%2C%22ca_n%22%3A%22-%22%2C%22ca_i%22%3A%22-%22%2C%22sid%22%3A53804476135%7D; Hm_lpvt_936a6d5df3f3d309bda39e92da3dd52f=1600585005',
        'Referer':'https://www.guazi.com/cd/buy/'
}
url_detail='https://www.guazi.com/cd/buy/o2'
res=requests.get(url=url_detail,headers=headers)
html=etree.HTML(res.text)
ul=html.xpath('/html/body/div[6]/ul')[0]
kong=[]
for li in ul:
    if li.xpath('./a/@title') !=kong:
        title=li.xpath('./a/@title')[0]
    else:
        title="待定"
    if li.xpath('./a/div[2]/p/text()') !=kong:
        price=li.xpath('./a/div[2]/p/text()')[0]
    else:
        price="未知"
    if li.xpath('./a/div[1]/text()[1]') !=kong:
        year=li.xpath('./a/div[1]/text()[1]')[0]
    else:
        year="未知"
    if li.xpath('./a/div[1]/text()[2]')!=kong:
        km=li.xpath('./a/div[1]/text()[2]')[0]
    else:
        km="未知"
    if len(li.xpath('./a/div[2]/em/text()') )==0:
        old_price="0"
    else:
        old_price=li.xpath('./a/div[2]/em/text()')[0]
    print(title,price,old_price,year,km)


如上, 使用了很多if---else 语句。看怎么样优化下一下。
之所以这样,是因为,偶尔某些项目找不到会报错(out of range)
谢谢
最佳答案
2020-9-20 16:40:44
可以试着这么写,会减少点代码
for li in ul:
    title = li.xpath('./a/@title')
    title = title[0] if len(title) > 0 else "待定"
    price = li.xpath('./a/div[2]/p/text()')
    price = price[0] if len(price) > 0 else "未知"
    year = li.xpath('./a/div[1]/text()[1]')
    year = year[0] if len(year) > 0 else "未知"
    km = li.xpath('./a/div[1]/text()[2]')
    km = km[0] if len(km) > 0 else "未知"
    old_price = li.xpath('./a/div[2]/em/text()')
    old_price = old_price[0] if len(old_price) > 0 else "0"

    print(title, price, old_price, year, km)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-9-20 16:40:44 | 显示全部楼层    本楼为最佳答案   
可以试着这么写,会减少点代码
for li in ul:
    title = li.xpath('./a/@title')
    title = title[0] if len(title) > 0 else "待定"
    price = li.xpath('./a/div[2]/p/text()')
    price = price[0] if len(price) > 0 else "未知"
    year = li.xpath('./a/div[1]/text()[1]')
    year = year[0] if len(year) > 0 else "未知"
    km = li.xpath('./a/div[1]/text()[2]')
    km = km[0] if len(km) > 0 else "未知"
    old_price = li.xpath('./a/div[2]/em/text()')
    old_price = old_price[0] if len(old_price) > 0 else "0"

    print(title, price, old_price, year, km)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-1-18 15:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表