Python-orm: peewee - minor changes to get a script up and ru

Einfach Dinge, die nichts mit XAMPP, Apache Friends, Apache, MySQL, PHP und alle dem zu tun haben. Allerlei halt. ;)

Python-orm: peewee - minor changes to get a script up and ru

Postby unleash » 16. July 2014 17:35

hello - relatively new to python i have some issues

Code: Select all
import urllib
import urlparse
import re
# import peewee
import json
from peewee import *

#from peewee import MySQLDatabase ('cpan', user='root',passwd='rimbaud')

db = MySQLDatabase('cpan', user='root',passwd='rimbaud')

class User(Model):
    name = TextField()
    cname = TextField()
    email = TextField()
    url = TextField()

    class Meta:
        database = db # this model uses the cpan database

User.create_table() #ensure table is created

url = ""
html = urllib.urlopen(url).read()
for lk, capname, name in re.findall('<a href="(/~.*?/)"><b>(.*?)</b></a><br/><small>(.*?)</small>', html):
    alk = urlparse.urljoin(url, lk)

    data = { 'url':alk, 'name':name, 'cname':capname }

    phtml = urllib.urlopen(alk).read()
    memail ='<a href="mailto:(.*?)">', phtml)
    if memail:
        data['email'] =

data = json.load('email') #your json data file here

for entry in data: #assuming your data is an array of JSON objects
    user = User.create(name=entry["name"], cname=entry["cname"],
        email=entry["email"], url=entry["url"])

but this fails... see

Code: Select all
martin@linux-70ce:~/perl> python
Traceback (most recent call last):
  File "", line 47, in <module>
    data = json.load('email') #your json data file here
  File "/usr/lib/python2.7/json/", line 286, in load
    return loads(,
AttributeError: 'str' object has no attribute 'read'

guess that i have made something wrong: We should try out to write like so:

Code: Select all
data = []
before our html is going to parse and loop and then do
Code: Select all
entry = { 'url':alk, 'name':name, 'cname':capname }

and probably
Code: Select all
within the loop.

if you have some ideas - i would be glad to hear from you
Posts: 141
Joined: 03. December 2011 10:16
Operating System: OpenSuse Linux 12.1

Return to Allerlei

Who is online

Users browsing this forum: No registered users and 2 guests