如何清理此脚本的响应以使其更具可读性?

如何将该脚本的输出转换为更简洁的格式(如 csv)?当我将回复保存为文本时,它的格式很糟糕。我尝试使用 writer.writerow 但无法使用此方法来解释变量。


import requests

from bs4 import BeautifulSoup



url = "https://www.rockauto.com/en/catalog/ford,2015,f-150,3.5l+v6+turbocharged,3308773,brake+&+wheel+hub,brake+pad,1684"


response = requests.get(url)

data = response.text

soup = BeautifulSoup(data, 'html.parser')


meta_tag = soup.find('meta', attrs={'name': 'keywords'})


category = meta_tag['content']


linecodes = []

partnos = []

descriptions = []

infos = []

for tbody in soup.select('tbody[id^="listingcontainer"]'):

    tmp = tbody.find('span', class_='listing-final-manufacturer')

    linecodes.append(tmp.text if tmp else '-')


    tmp = tbody.find('span', class_='listing-final-partnumber as-link-if-js buyers-guide-color')

    partnos.append(tmp.text if tmp else '-')


    tmp = tbody.find('span', class_='span-link-underline-remover')

    descriptions.append(tmp.text if tmp else '-')


    tmp = tbody.find('div', class_='listing-text-row')

    infos.append(tmp.text if tmp else '-')



for row in zip(linecodes,partnos,infos,descriptions):

    result = category + ' | {:<20} | {:<20} | {:<80} | {:<80}'.format(*row)

    with open('complete.txt', 'a+') as f:

        f.write(result + '/n')

        print(result)


繁星点点滴滴
浏览 104回答 1
1回答

繁华开满天机

你可以将它放入 pandas 数据框中for-loop从原始代码中删除最后一个。# importsimport requestsfrom bs4 import BeautifulSoupimport pandas as pd# set pandas display options to display more rows and columnspd.set_option('display.max_columns', 700)pd.set_option('display.max_rows', 400)pd.set_option('display.min_rows', 10)# your codeurl = "https://www.rockauto.com/en/catalog/ford,2015,f-150,3.5l+v6+turbocharged,3308773,brake+&+wheel+hub,brake+pad,1684"response = requests.get(url)data = response.textsoup = BeautifulSoup(data, 'html.parser')meta_tag = soup.find('meta', attrs={'name': 'keywords'})category = meta_tag['content']linecodes = []partnos = []descriptions = []infos = []for tbody in soup.select('tbody[id^="listingcontainer"]'):&nbsp; &nbsp; tmp = tbody.find('span', class_='listing-final-manufacturer')&nbsp; &nbsp; linecodes.append(tmp.text if tmp else '-')&nbsp; &nbsp; tmp = tbody.find('span', class_='listing-final-partnumber as-link-if-js buyers-guide-color')&nbsp; &nbsp; partnos.append(tmp.text if tmp else '-')&nbsp; &nbsp; tmp = tbody.find('span', class_='span-link-underline-remover')&nbsp; &nbsp; descriptions.append(tmp.text if tmp else '-')&nbsp; &nbsp; tmp = tbody.find('div', class_='listing-text-row')&nbsp; &nbsp; infos.append(tmp.text if tmp else '-')添加了数据框的代码# create dataframedf = pd.DataFrame(zip(linecodes,partnos,infos,descriptions), columns=['codes', 'parts', 'info', 'desc'])# add the category columndf['category'] = category# break the category column into multiple columns if desired# skip the last 2 columns, because they are emptydf[['cat_desc', 'brand', 'model', 'engine', 'cat_part']] = df.category.str.split(',', expand=True).iloc[:, :-2]# drop the unneeded category columndf.drop(columns='category', inplace=True)# save to csvdf.to_csv('complete.txt', index=False)# display(df)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; codes&nbsp; &nbsp; &nbsp; &nbsp;parts&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; info&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;desc&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cat_desc&nbsp; brand&nbsp; &nbsp;model&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;engine&nbsp; &nbsp; cat_part0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CENTRIC&nbsp; &nbsp; 30016020&nbsp; Rear; w/ Manual parking brake&nbsp; &nbsp;Semi-Metallic; w/Shims and Hardware&nbsp; 2015 FORD F-150 Brake Pad&nbsp; &nbsp;FORD&nbsp; &nbsp;F-150&nbsp; &nbsp;3.5L V6 Turbocharged&nbsp; &nbsp;Brake Pad1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CENTRIC&nbsp; &nbsp; 30116020&nbsp; Rear; w/ Manual parking brake&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Ceramic; w/Shims and Hardware&nbsp; 2015 FORD F-150 Brake Pad&nbsp; &nbsp;FORD&nbsp; &nbsp;F-150&nbsp; &nbsp;3.5L V6 Turbocharged&nbsp; &nbsp;Brake Pad2&nbsp; DYNAMIC FRICTION&nbsp; 1551160200&nbsp; &nbsp; &nbsp;Rear; Manual Parking Brake&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 5000 Advanced; Ceramic&nbsp; 2015 FORD F-150 Brake Pad&nbsp; &nbsp;FORD&nbsp; &nbsp;F-150&nbsp; &nbsp;3.5L V6 Turbocharged&nbsp; &nbsp;Brake Pad
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python