Replace a python string with multiple characters at a time

Source: Internet
Author: User

I recently used python to improve efficiency and save time for my SEO work. I found that python is really good and can accomplish many things.

The following figure shows how to automatically update a website using python.ArticleThe replacement of the anchor text is also a good application for replacing multiple characters with a python string at a time.

The main idea is: there are some characters in an article that need to be used as the anchor text. My goal is to replace the characters with links and automatically add external links to the website, and for the diversity of links,

I used several candidate links to randomly set randomProgramChoose your own. If you know Seo, you should be clear about why.

Next I will mainly talk about the python program used. I hope you will have a good understanding of the need to replace multiple characters in another string.

I first defined a dictionary: The dictionary key mainly defines the keywords to be replaced, and the list below is

Linkcontent = {'keyword 1': ['link 1 (anchor text 1) ', 'link 2 (anchor text 2)'], 'keyword 2 ': ['link 1 (anchtext 1) ', 'link 2 (anchtext 2)'],

'Keyword 3': ['link 1 (anchor text 1) ', 'link 2 (anchor text 2)'], 'keyword 4 ': ['link 1 (anchtext 1) ', 'link 2 (anchtext 2 )','..... ', you need to add this keyword to replace the possibility of the anchor text

This ensures the diversity of external links.]

......

}

Definition Method

Def Multiple_replace (text, adict, num ):
'''Replace multiple times '''
RX = Re. Compile ( '|' . Join (MAP (Re. Escape, adict )))

Def One_xlat (MATCH ):
Rnum = Len (adict [match. Group ( 0 )])
Sortid = random. Choice (range (rnum )) # Random category
Return Adict [match. Group ( 0 )] [Sortid]

IfNum %2=0:
Textnum =3
Else:
Textnum =4

ReturnRx. sub (one_xlat, text, textnum)

Import re import random

Call: zcontent = multiple_replace (content, linkcontent, specified number of times)

Because I use a for each loop here, for I in range (300 ):

Zcontent = multiple_replace (content, linkcontent, I) in this way, we can generate three connections in half of the content in the article, and four in half.

If you only test the string replacement function, you can retain only the content and linkcontent parameters, and the specified number of times is unnecessary.

If you have any questions, please refer to the comments below. To implement this function, you can understand the multiple_replace function.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.