Skip to content

Instantly share code, notes, and snippets.

@globalmac
Last active July 31, 2018 11:01
Show Gist options
  • Save globalmac/05433a779c78475cbb6175991a211c41 to your computer and use it in GitHub Desktop.
Save globalmac/05433a779c78475cbb6175991a211c41 to your computer and use it in GitHub Desktop.
Пример парсинга с помощью lxml и psycopg2 для Postgres
#!/usr/local/bin/python
# coding: utf8
import urllib as url
from lxml import etree
import psycopg2
import datetime
print ("Обработка файла = %s" % datetime.datetime.now())
tree = etree.parse('-1.xml')
offers = tree.xpath('/yml_catalog/shop/offers/offer')
print ("Импорт в БД = %s" % datetime.datetime.now())
connect = psycopg2.connect(database='DBNAME', user='DBUSER', host='DBHOST', password='DBPASS')
cursor = connect.cursor()
for offer in offers:
name = offer.find('name').text
#available = offer.get('available') #attr
id = offer.get('id')
#print id, name
cursor.execute('INSERT INTO table (name, pid) VALUES (%s, %s)', (name, id))
connect.commit()
connect.close()
print ("Завершение импорта = %s" % datetime.datetime.now())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment