#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Posts
    354
    Rep Power
    0

    obstacles with the syntax ....


    Code:
    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
    
    data_array = []
    
    url = "http://search.cpan.org/author/?W"
    html = urllib.urlopen(url).read()
    
    for lk, capname, name in re.findall('<a rel="nofollow" href="(/~.*?/)"><b>(.*?)</b></a><br/><small>(.*?)</small>', html):
    
        print lk, capname, name
        alk = urlparse.urljoin(url, lk)
    
        data = { 'url':alk, 'name':name, 'cname':capname }
     
        phtml = urllib.urlopen(alk).read()
        memail = re.search('<a href="mailto:(.*?)">', phtml)
        if memail:
            data['email'] = memail.group(1)
        
        data_array.append (data)
            
    # data = json.load('emailyourn 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"])
        user.save()



    Code:
    Traceback (most recent call last):
      File "cpan_3000.py", line 53, in <module>
        user = User.create(name=entry["name"], cname=entry["cname"],
    TypeError: string indices must be integers, not str
    martin@linux-70ce:~/perl> ^C
    martin@linux-70ce:~/perl>


    The two biggest obstacles for new programmers are often:

    syntax errors
    and thinks alike

    i try to figure out why the issues arise[/QUOTE]
  2. #2
  3. Contributing User

    Join Date
    Aug 2011
    Posts
    5,202
    Rep Power
    483
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo