2016-05-18 7 views
0

最初のテキストセットをWebサイトから取得した後にカンマで1行にフォーマットしてから価格を引き上げるのに問題があります。私がしようとしているのは、製品情報の後にカンマで区切られた製品情報と価格を1行に入れてExcelスプレッドシートにインポートできるようにすることです。すべての手がかりは?ありがとうございますPythonは1行にCSVファイルをフォーマットします

import requests 
from bs4 import BeautifulSoup 
import csv 

b6 = open('sears.csv', 'w', newline='') 
a6 = csv.writer(b6,delimiter=',') 

soup = BeautifulSoup(requests.get("http://www.sears.ca/catalog/appliances-fridges-freezers-refrigerators-top-freezer-en-wp-836#facet:&productBeginIndex:0&orderBy:&pageView:grid&minPrice:&maxPrice:&pageSize:100&").text) 

g6_data = soup.select("div.product_name a") 
p6_data = soup.select("div.product_price") 

for g6, p6 in zip(g6_data, p6_data): 
    c6 = (g6.text, p6.text) 
    print(g6.text, p6.text) 
    a6.writerow(c6) 

b6.close() 

答えて

0

お客様のリクエストはOKです。フィルタリングはBeautifulSoupではありません。

import requests 
from BeautifulSoup import BeautifulSoup 

soup = BeautifulSoup(requests.get("http://www.sears.ca/catalog/appliances-fridges-freezers-refrigerators-top-freezer-en-wp-836#facet:&productBeginIndex:0&orderBy:&pageView:grid&minPrice:&maxPrice:&pageSize:100&").text) 

g_descrs = [i.find("a").text for i in soup.findAll("div", {"class": "product_name", "itemprop": True})] 
g_prices = [i.find("input").get('value') for i in soup.findAll("div", {"class": "product_price"})] 

rows = map(lambda x: '%s;%s' % x, zip(g_descrs, g_prices)) 

with open('./result.csv', 'w') as result: 
    for row in rows: 
     result.write(row) 
+0

申し訳ありませんが、実際にはこれで表示されるコンテンツはありません。 – nobb666

+0

うわー、私はちょうどここでテストし、動作します! –

+0

私は自分のコード全体を投稿しました。私はPython 3.5を使用しています。まだ何も表示されません – nobb666

関連する問題