0

我被困在 ScraperWiki 中的这个刮刀上。我只想要 dir='ltr' 的 ul 中的 li 元素中的文本。我每周运行这个脚本,句子可能彼此相似,但又是一个全新的句子。这就是为什么我想在我的数据中包含日期。

import scraperwiki
from mechanize import Browser
import lxml.html
from datetime import date, timedelta, datetime
from scraperwiki import sqlite

datum = date.today()
print datum

url = 'http://www.knvb.nl/nieuws/excessenav/actueel'
mech = Browser()
page = mech.open(url)
tree = lxml.html.fromstring(page.read())

# print tree.xpath('//ul[@dir="ltr"]/li') # access li elements
# print tree.xpath('//ul[@dir="ltr"]/li/text()') # access text in li elements

uitspraak = tree.xpath("//ul[@dir='ltr']/li/text()")
print uitspraak

# Saving data:
unique_keys = [ 'datum', 'uitspraak' ]
data = { 'datum':datum, 'uitspraak':uitspraak  }
scraperwiki.sql.save(unique_keys, data)

我收到以下错误:

Traceback (most recent call last):
  File "./code/scraper", line 28, in <module>
    scraperwiki.sql.save(unique_keys, data)
  File "/usr/local/lib/python2.7/dist-packages/scraperwiki/sqlite.py", line 34, in save
    return dt.upsert(data, table_name = table_name)
  File "/usr/local/lib/python2.7/dist-packages/dumptruck/dumptruck.py", line 301, in upsert
    self.insert(upsert=True, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/dumptruck/dumptruck.py", line 284, in insert
    self.execute(sql, values, commit=False)
  File "/usr/local/lib/python2.7/dist-packages/dumptruck/dumptruck.py", line 138, in execute
    raise self.sqlite3.InterfaceError(unicode(msg) + '\nTry converting types or pickling.')
sqlite3.InterfaceError: Error binding parameter 0 - probably unsupported type.
Try converting types or pickling.

我怎样才能让这个刮刀保存我的数据?

4

1 回答 1

2

这给出了一个结果列表,该列表是导致不受支持的类型错误的原因:

tree.xpath("//ul[@dir='ltr']/li/text()")

您必须改为存储每个结果:

unique_keys = ['datum', 'uitspraak']
for x in uitspraak:
  data = {'datum': datum, 'uitspraak': x}
  scraperwiki.sql.save(unique_keys, data)

可能有更好的方法来存储多个结果——我以前从未使用过 scraperwiki 并且不知道 API。

于 2013-11-06T11:09:51.040 回答