0

I like to split this url:

https://ordbok.uib.no/perl/ordbok.cgi?startpos=1&ant_bokmaal=100&ant_nynorsk=5&antall_vise=1&OPP=+n1&ordbok=bokmaal&bokmaal=%2B&spraak=bokmaal

So it should be:

https://ordbok.uib.no/perl/ordbok.cgi?startpos=1&ant_bokmaal=

and

&ant_nynorsk=5&antall_vise=1&OPP=+n1&ordbok=bokmaal&bokmaal=%2B&spraak=bokmaal

because i lik to change the number 100 to come to the next page.

How can i have a variable in between there?

NorGer
  • 67
  • 1
  • 2
  • 9
  • Possible duplicate of [Best way to parse a URL query string](https://stackoverflow.com/questions/10113090/best-way-to-parse-a-url-query-string) – Maurice Meyer Jul 01 '19 at 12:35
  • 1
    Can't imagine it's only about this single url, isn't it? If you want to split just this specfic url, you could just use `.split('100')` to get a list where the two elements are both of your desired outputs. – trotta Jul 01 '19 at 12:37
  • you can change order and put `&ant_bokmaal=` at the end of url and append number without spliting. Or use text formating or f-string to generate url. – furas Jul 01 '19 at 12:39

3 Answers3

1

Use format() to pass variable in it.

def url(index):
 url="https://ordbok.uib.no/perl/ordbok.cgi?startpos=1&ant_bokmaal={}&ant_nynorsk=5&antall_vise=1&OPP=+n1&ordbok=bokmaal&bokmaal=%2B&spraak=bokmaal".format(index)
 print(url)
KunduK
  • 32,888
  • 5
  • 17
  • 41
0

If it is just this very specific replacement you can use the string replace function.

s="https://ordbok.uib.no/perl/ordbok.cgi?startpos=1&ant_bokmaal=100&ant_nynorsk=5&antall_vise=1&OPP=+n1&ordbok=bokmaal&bokmaal=%2B&spraak=bokmaal"

Replace:

s.replace("ant_bokmaal=100", "ant_bokmaal=111")

Result:

https://ordbok.uib.no/perl/ordbok.cgi?startpos=1&ant_bokmaal=111&ant_nynorsk=5&antall_vise=1&OPP=+n1&ordbok=bokmaal&bokmaal=%2B&spraak=bokmaal'

you can see that ant_bokmall changed to 111.

klaas
  • 1,661
  • 16
  • 24
0

I would suggest to use re, which is quicker, and more accurate:

>>> url = "https://ordbok.uib.no/perl/ordbok.cgi?startpos=1&ant_bokmaal=100&ant_nynorsk=5&antall_vise=1&OPP=+n1&ordbok=bokmaal&bokmaal=%2B&spraak=bokmaal"
>>> re.sub(r'(?<=ant\_bokmaal\=)\d+', str(999), url)
'https://ordbok.uib.no/perl/ordbok.cgi?startpos=1&ant_bokmaal=999&ant_nynorsk=5&antall_vise=1&OPP=+n1&ordbok=bokmaal&bokmaal=%2B&spraak=bokmaal'
sashaboulouds
  • 1,566
  • 11
  • 16