Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
578 views
in Technique[技术] by (71.8m points)

mysql - convert list to string to insert into my sql in one row in python scrapy

I want to convert a list object into a string and insert this string as one row in mysql database. Can someone please provide a solution to this. My code looks like this:

def parse(self, response):
    hxs = HtmlXPathSelector(response)
    sites = hxs.select('//ul/li')
    for site in sites:
         con = mysqldb.connect(
                    host="localhost",
                    user="dreamriks",
                    passwd="dreamriks",
                    db="scraped_data"
                 )
         cur = con.cursor()
         quest = site.select('//h2').extract()
         ans = site.select('//h3').extract()
         meta = site.select('//meta').extract()
         cur.execute("""Insert into scraped_data(h2, h3, meta) Values(%s,%s,%s)""",(quest,ans,meta))                   
         con.commit()
         con.close()

The code above gives the following error:

File "/usr/local/lib/python2.7/dist-packages/Scrapy-0.14.0.2841-py2.7.egg/scrapy/spider.py", line 62, in parse
        raise NotImplementedError
    exceptions.NotImplementedError:

Can someone help me with this error. I am stuck at this.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
l = ['foo', 'bar', 'baz']
ls = ", ".join(l)
# ls == "foo, bar, baz"

Now you can insert this string into your database.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...