Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Usage of format()
#1
Hello,

The following code is about web spider, it is from a python tutorial, but my question is irrelevnat to web spider, but a general question of python.

I'd like to know the usage of format() in the following code.

The usage of format I learnt is string with {} as placeholder and joined by format(argument, argument...).
e.g. My name is {}, I am (). format('peter', 18)

I don't understand #18, the usage of format(self.url%self.pagenum). what's the function of % in #18?

(The purpose of the format is to use the url template to join the webpage number, so create a new url)
# -*- coding: utf-8 -*-
import scrapy

class XiaohuaSpider(scrapy.Spider):
    name = 'xiaohua'
    # allowed_domains = ['www.xxx.com']
    start_urls = ['http://www.521609.com/meinvxiaohua/']

    url = 'http://www.521609.com/meinvxiaohua/list12%d.html'
    page_num = 2

    def parse(self, r.esponse):
        li_list = response.xpath('//*[@id="content"]/div[2]/div[2]/ul/li')
        for li in li_list:
            img_name = li.xpath('./a[2]/b/text() | ./a[2]/text()').extract_first()
            print(img_name)

        if self.page_num <= 11:
            new_url = format(self.url%self.page_num)
            self.page_num += 1
            yield scrapy.Request(url=new_url,callback=self.parse)
Reply
#2
Use f'string
new_url = f'self.url{self.page_num}'

or format().
new_url = 'self.url{)'.format(self.page_num)

Do not use the old % formatting
new_url = 'self.url%d' % (self.page_num)

In the old formatting, the % did double duty. The % in the format string marks a replacement field, and the % after the string is a delimiter between the format string and the replacement values. I think this is really clunky and doesn't look like it should be valid Python.

The newer format() is a little better. The curly brackets do a nice job indicating the extent of the replacement field. But the values are too far away and if you have multiple values you have to think about which value is associated with which replacement field.

The newer f'string does a nice job putting the values where they belong. I think it is very readable and it is shorter than using .format().
Reply
#3
Thank you very much for your help.

The code I quote in post 1 is from a tutorial I am learning. I didn't understand what the code of format he wrote. Now I got it.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020