看破、点不破
2022-07-20 16:54
采纳率: 100%
浏览 23

python爬虫数据显示问题



```python
from pyquery import PyQuery as pq
import requests
import csv

def download_page_source(url):   #请求源代码
 resp = requests.get(url)
 resp.encoding = "utf-8"
 return resp.text
#  return resp.text
dic ={}

def parse_data(source):
    query = pq(source)
    divs = query(".cm-content-box").items()
    for div in divs:
        chezhuname = div("div>div>a>div>div>span:nth-child(1)").text()
        chexing = div("div > a >div:nth-child(1)>div:nth-child(1)>p").text()
        carmoney= div("div > a >div:nth-child(1)>div:nth-child(1)>div>span:nth-child(2)>span:nth-child(2)").text()
        youhao = div("div > a >div:nth-child(1)>div:nth-child(1)>div>span:nth-child(3)").text()
        time =div("div > a >div:nth-child(1)>div:nth-child(1)>div>span:nth-child(4)>span:nth-child(2)").text()
        print(time,chezhuname)
 
        
     def main(url):
    page_source = download_page_source(url) 
    parse_data(page_source)

if __name__ == '__main__':
 url = "https://dianping.yiche.com/songplusdm/koubei/"

 main(url)



如何将爬虫到的数据一行一行对应出来,并且保存到csv格式文件中
格式如下:
车主:新宋婆 车型:宋PLUS新能源 2021款 DM-i 1.5L 两驱 旗舰PLUS 裸车价:19.98万 油耗 2.8L/100km 购车时间:2022-01


我现在输出单个数据的显示不是一一对应的,而是散乱的。

如图:
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/242613703856129.png "#left")

3条回答 默认 最新

相关推荐 更多相似问题