Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Applying String Methods | Getting Acquainted with HTML
Web Scraping with Python

bookApplying String Methods

メニューを表示するにはスワイプしてください

What can you do with the page you have read? Since it is a string, you can use any string method. For example, the .find() method returns the index of the first occurrence of a specific element. You can use it to locate the page title by finding the indexes of the opening and closing tags and considering the length of the closing tag.

1234567891011121314
# Importing the module from urllib.request import urlopen # Opening web page url = "https://codefinity-content-media.s3.eu-west-1.amazonaws.com/18a4e428-1a0f-44c2-a8ad-244cd9c7985e/mother.html" page = urlopen(url) # Reading and decoding web_page = page.read().decode("utf-8") # Indexes of opening and closing title tags start = web_page.find("<title") finish = web_page.find("</title>") + len("</title>") print(web_page[start:finish])
copy

As shown in the example above, two variables, start and finish, were created. The start variable stores the index of the first character within the opening <title> tag, while the finish variable stores the index of the character right after the closing </title> tag. The .find() method returns the starting index of the closing tag, so the tag’s length is added to get the final position.

Note
Study More

List slicing excludes the last element, which is why the next character after the closing tag is used.

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  10

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 1.  10
some-alt