import requests
import lxml.html

from person.models import Person
from parliament.models import PoliticalParty
from parliament.models import PartyMember
from parliament.models import ParliamentMember


def create_members():
    url = 'http://www.tweedekamer.nl/kamerleden/alle_kamerleden'
    page = requests.get(url)
    tree = lxml.html.fromstring(page.content)

    rows = tree.xpath("//tbody/tr")

    for row in rows:
        columns = row.xpath("td")
        if len(columns) == 8:
            surname = columns[0][0].text.split(',')[0]
            print(surname)
            prefix = columns[0][0].text.split('.')[-1].strip()
            forename = columns[1][0].text
            if Person.person_exists(forename, surname):
                continue
            party_name = columns[2][0].text
            party = PoliticalParty.get_or_create_party(party_name)
            # residence = columns[3][0].text
            # age = columns[4][0][0].text
            # sex = columns[5][0].text
            # assert age is not None
            # if sex == 'Man':
            #     sex = Member.MALE
            # elif sex == 'Vrouw':
            #     sex = Member.FEMALE
            person = Person.objects.create(
                forename=forename,
                surname=surname,
                surname_prefix=prefix,
            )
            party_member = PartyMember.objects.create(person=person, party=party)
            # parliament_member = ParliamentMember.objects.create(person=person)
            print("new person: " + str(person))
            print("new party member: " + str(party_member))
            # print("new parliament member: " + str(parliament_member))
@@ -60,16 +60,57 @@
  Person%0A
+from parliament.models import Parliament%0A
 from par
@@ -400,16 +400,63 @@
 ntent)%0A%0A
+    parliament = get_or_create_tweede_kamer()%0A%0A
     rows
@@ -832,16 +832,274 @@
     
-continue
+person = Person.objects.get(forename=forename, surname=surname)%0A            else:%0A                person = Person.objects.create(%0A                    forename=forename,%0A                    surname=surname,%0A                    surname_prefix=prefix,%0A                )
 %0A   
@@ -1513,173 +1513,8 @@
 ALE%0A
-            person = Person.objects.create(%0A                forename=forename,%0A                surname=surname,%0A                surname_prefix=prefix,%0A            )%0A
     
@@ -1602,18 +1602,16 @@
         
- #
  parliam
@@ -1668,16 +1668,39 @@
 n=person
+, parliament=parliament
 )%0A      
@@ -1812,18 +1812,16 @@
         
- #
  print(%22
@@ -1862,16 +1862,272 @@
 liament_member))
+%0A%0A%0Adef get_or_create_tweede_kamer():%0A    parliaments = Parliament.objects.filter(name='Tweede Kamer')%0A    if parliaments.exists():%0A        return parliaments%5B0%5D%0A    else:%0A        return Parliament.objects.create(name='Tweede Kamer', wikidata_uri='Q233262')

Create tweede kamer model when creating parliament members