Python正則部分

b806071099發表於2020-11-16

1、正則替換

url_1 = 'A%E7%94%B1%E8%A1%8C&fhLimit=0%2C60&hlFields=title&isTouch=0&lines6To10=0&lm=1800%2C60&m=l%2CbookingInfo%2CbrowsingInfo%2Clm&o=pop-desc&random=982944&s=all&sourcepage=list&t=all&tf=pcindex_free&tm=djnull&userId=00005c80306c29d3c5d0e4c0'
num_count = 10000
a = re.search(r'lm=(\d+?)%2C60', url_1).group(0)
page_num = re.search(r'lm=(\d+?)%2C60', url_1).group(1)
next_page_num = int(page_num) + 60
if next_page_num < num_count:
    new_a = f'lm={next_page_num}%2C60'
    # a為 要被替代的部分,new_a為 新替換的字串部分,url_1為 整體字串
    next_page_url = re.sub(a, new_a, url_1)
    print(next_page_url)

相關文章