簡體   English   中英

使用特定列將抓取的數據導出到 CSV

[英]exporting scraped data to CSV with a specific column

我的代碼目前正在命令屏幕上打印結果。

期望的結果(見附件截圖):將最終輸出寫入 'a2' 列中的 CSV 文件並將 sku# 輸出到列 'a1' sku# 始終是 url 中第 5 個 '/' 之后的文本

這是代碼

from bs4 import BeautifulSoup
import urllib.request
import csv
def get_bullets(url):
    page = urllib.request.urlopen(url)
    soup = BeautifulSoup(page,'lxml')
    content = soup.find('div', class_='js-productHighlights product-highlights c28 fs14 js-close')
    bullets = content.find_all('li', class_='top-section-list-item')
    for bullet in bullets:
     print(bullet.string)

get_bullets('https://www.bhphotovideo.com/c/product/1225875-REG/canon_1263c004_eos_80d_dslr_camera.html')

預期結果:

在此處輸入圖片說明

謝謝!

from bs4 import BeautifulSoup
import urllib.request
import pandas as pd


def get_bullets(url):
    sku = url.split('/')[5]
    page = urllib.request.urlopen(url)
    soup = BeautifulSoup(page,'lxml')
    content = soup.find('div', class_='js-productHighlights product-highlights c28 fs14 js-close')
    bullets = content.find_all('li', class_='top-section-list-item')

    bullets_text = '\n'.join([ bullet.text for bullet in bullets ])

    temp_df = pd.DataFrame([[sku, bullets_text]], columns = ['sku','bullets'])
    temp_df.to_csv('path/filename.csv', index=False)


get_bullets('https://www.bhphotovideo.com/c/product/1225875-REG/canon_1263c004_eos_80d_dslr_camera.html')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM