![]() ![]() decode ( "utf-8" ) pattern = ".*?" match_results = re. Import re from urllib.request import urlopen url = "" page = urlopen ( url ) html = page. You need a more reliable way to extract text from HTML. These sorts of problems can occur in countless unpredictable ways. This means that html returns all the HTML starting with that newline and ending just before the tag. The character at index 6 of the string html is a newline character ( \n) right before the opening angle bracket ( tag. When -1 is added to len(""), which is 7, the start_index variable is assigned the value 6. Html.find("") returns -1 because the exact substring "" doesn’t exist. The opening tag has an extra space before the closing angle bracket ( >), rendering it as. The HTML for the /profiles/poseidon page looks similar to the /profiles/aphrodite page, but there’s a small difference. Whoops! There’s a bit of HTML mixed in with the title. find ( "" ) > title = html > title '\n\nProfile: Poseidon' find ( "" ) len ( "" ) > end_index = html. > url = "" > page = urlopen ( url ) > html = page. find() returns the index of the first occurrence of a substring, you can get the index of the opening tag by passing the string "" to. If you know the index of the first character of the title and the first character of the closing tag, then you can use a string slice to extract the title. Let’s extract the title of the web page you requested in the previous example. find() to search through the text of the HTML for the tags and extract the title of the web page. One way to extract information from a web page’s HTML is to use string methods. Extract Text From HTML With String Methods Once you have the HTML as text, you can extract information from it in a couple of different ways. print ( html ) Profile: Aphrodite Name: Aphrodite Favorite animal: Dove Favorite color: Red Hometown: Mount Olympus ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |