2014-09-05 182 views
0

我有一個基本的Python腳本,可以將輸出存儲到文件中。這是很難解析的文件。 有什麼其他的方法可以將抓取的數據寫入文件,這些文件可以輕鬆讀入Python進行分析?Python Web Scrape將輸出寫入文件

import requests 
from bs4 import BeautifulSoup as BS 
import json 
data='C:/test.json' 
url="http://sfbay.craigslist.org/search/sby/sss?sort=rel&query=baby" 

r=requests.get(url) 
soup=BS(r.content) 
links=soup.find_all("p") 
#print soup.prettify() 

for link in links: 
    connections=link.text 
    f=open(data,'a') 
    f.write(json.dumps(connections,indent=1)) 
    f.close() 

輸出文件包含此: 「$ 09月25日5瓷娃娃鹿$ 25(桑尼維爾)PIC家居用品 - 由業主 」「 7500 $ 9月5日GEORGE STECK寶貝大PLAYER PIANO $ 7500(摩根山)映射樂器 - 通過

回答

0

這聽起來像你的問題更多的是如何解析你從Craigslist網站獲得刮數據,而不是如何處理文件。一種方法是取每個<p>元素並用空格標記字符串。例如,令牌化字符串

「$ 09月25日5瓷娃娃鹿$ 25(桑尼維爾)PIC家居用品 - 由業主」 使用split

可以做到:

s = " $25 Sep 5 Porcelain Baby Deer $25 (sunnyvale) pic household items - by owner " 
L = s.strip().split(' ') #remove whitespace at ends and break string apart by spaces 

L現在是一個值爲的列表

['$25', 'Sep', '5', 'Porcelain', 'Baby', 'Deer', '$25', '(sunnyvale)', 'pic', 'household', 'items', '-', 'by', 'owner'] 

從這裏您可以嘗試通過它們出現的順序來確定列表元素的含義。 L[0]可能會始終保持價格,月份爲L[1],月份爲L[2],等等。如果您有興趣將這些值寫入文件並稍後再解析,請考慮閱讀csv module

+0

謝謝大家。這對我來說很有用 – 2014-09-11 04:58:52

1

如果你想將它從Python中寫入一個文件,並重新讀取到蟒蛇後,您可以使用味酸 - Pickle Tutorial

味酸文件是二進制的,不會是人類可讀的,如果這對你很重要,那麼你可以看看yaml,我會承認它有一點學習曲線,但會產生很好的格式化文件。

import yaml 

f = open(filename, 'w') 
f.write(yaml.dump(data)) 
f.close() 

... 


stream = open(filename, 'r') 
data = yaml.load(stream) 
+0

這將使@Amrita張以芳來存儲對象的狀態,但我不認爲它得到問題的心臟,這是寫入數據的一個好辦法,這樣可以很容易地解析之後由Python發佈。誠然,這個問題的範圍有點廣泛。 – Aphid 2014-09-05 19:36:49

+0

好點,我解釋爲「由Python解析」,意思是「從文件中讀取」,但你說得對,可能不是問題的意圖。在這種情況下,它是一個字符串操作問題,而不是文件IO問題。 – 2014-09-05 19:44:11

0
  1. 決定您實際需要的數據。價格?說明?列表日期?
  2. 決定一個好的數據結構來保存這些信息。我推薦一個包含相關字段或列表的類。
  3. 使用正則表達式或許多其他方法之一來擦除需要的數據。
  4. 把你不需要的東西扔掉

5a。寫列表內容到一個文件中,方便日後使用的格式(XML,逗號分隔等)

OR

5B。根據上面的Mike Ounsworth建議泡菜。

如果您還不熟悉XML解析,那麼只需爲每個鏈接編寫一行代碼並使用稍後可以使用的字符分隔所需字段。例如:

import re #I'm going to use regular expressions here 

link_content_matcher = re.compile("""\$(?P<price>[1-9]{1,4})\s+(?P<list_date>[A-Z]{1}[a-z]{2}\s+[0-9]{1,2})\s+(?P<description>.*)\((?P<location>.*)\)""") 

some_link = "$50 Sep 5 Baby Carrier - Black/Silver (san jose)" 

# Grab the matches 
matched_fields = link_content_matcher.search(some_link) 

# Write what you want to a file using a delimiter that 
# probably won't exist in the description. This is risky, 
# but will do in a pinch. 
output_file = open('results.txt', 'w') 
output_file.write("{price}^{date}^{desc}^{location}\n".format(price=matched_fields.group('price'), 
    date=matched_fields.group('list_date'), 
    desc=matched_fields.group('description'), 
    location=matched_fields.group('location'))) 
output_file.close() 

當您想重新訪問這些數據時,從文件中逐行抓取並使用split進行分析。

input_contents = open('results.txt', 'r').readlines() 

for line in input_contents: 
    price, date, desc, location = line.split('^') 
    # Do something with this data or add it to a list 
0
import requests 
from bs4 import BeautifulSoup as bs 
url="http://sfbay.craigslist.org/baa/" 
r=requests.get(url) 
soup=bs(r.content) 
import re 
s=soup.find_all('a', class_=re.compile("hdrlnk")) 
for i in s: 
    col=i.text 
    scol=str(col) 
    print scol 

s1=soup.find_all('span', class_=re.compile("price")) ### Price