Home > Back-end >  How to generate a url with python
How to generate a url with python

Time:06-21

I have some problem generating a website url. I want to use python to generate the url for each some phrases from the youdao website. I noticed that all the url for the phrases is in this format:

https://www.youdao.com/w/eng/**** *****/#keyfrom=dict2.top

All the * represent words for searching, the spaces in the words are replaced with . For example, the url for the phrase "good job" is:

https://www.youdao.com/w/eng/good job/#keyfrom=dict2.top

Currently, I have a csv file that looks like this:

enter image description here

How can I loop through this csv file and use it to generate the url for each phrase on youdao?

CodePudding user response:

Here, it is the solution :)

import random
import string
           #ID - Phrases
phrases = { 23:'good job', 
            56:'I love it',
            13:'looks good'
          }
list = list(phrases.items())
randomEntry = random.choice(list)

parsedEntry = randomEntry[1].replace(' ', ' ', 3)
url = "https://www.youdao.com/w/eng/"   parsedEntry  "#keyfrom=dict2.top"
print(url)

You haven't shared dict2 yet, so i did not touch this part. The output will be like:

https://www.youdao.com/w/eng/looks good kl#keyfrom=dict2.top

For automatic creating, you can simply use for loop:

           #ID - Phrases
phrases = { 23:'good job', 
            56:'I love it',
            13:'looks good'
          }
list = list(phrases.items())

for x in  range(6):
    randomEntry = random.choice(list)
    parsedEntry = randomEntry[1].replace(' ', ' ', 3)
    url = "https://www.youdao.com/w/eng/"   parsedEntry  "#keyfrom=dict2.top"
    print(url)

CodePudding user response:

first you have to read the phrases from csv file with it's module. then we have to make the URLs with the phrases, the phrases should encoding by urlencode to be possible to put them in the urls that urllib.parse.quote function will do this for us.

import csv
import urllib


# read phrases from csv file
with open('phrases.csv', 'r') as file:
    csv_reader = csv.reader(file)
    phrases = []
    header = None
    # add to list
    for row in csv_reader:
        # first row is header
        if not header :
            header = row
            continue
        phrases.append(row)

# make the urls
links = []

for item in phrases :
    phrase = item[1]
    # url encoding phrase
    ph_encoded = urllib.parse.quote(phrase)
    url = f"https://www.youdao.com/w/eng/{ph_encoded}/#keyfrom=dict2.top"
    # add new url
    links.append(url)

print(links)
  • Related