如何索引整个字符串而不是列表中的字母

2024-10-02 02:35:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图将一串单词添加到列表中,但当我尝试为该列表编制索引时,它会返回单个字母

例如:

url = 'https://almostginger.com/famous-movie-locations/'


titles = []
r = requests.get(url)
soup = BeautifulSoup(r.content, 'lxml')
titles = soup.select('h3')
for t in titles:
    tt = t.text.strip()
    for s in range(len(tt)):
        print(s)

显示仅为单个字母编制索引,而如果我尝试创建列表,则会出现以下错误:

titles.append(tt)

AttributeError: 'str' object has no attribute 'text'

预期成果:

'Café des Deux Moulins as seen in Amélie (2001)',
'Royal Palace of Caserta as seen in Angels and Demons (2009)'

Tags: textinhttpscomurl列表foras
2条回答

您会因为重复的变量名而出现错误。将其中一个titles更改为其他内容

import requests
from bs4 import BeautifulSoup
url = 'https://almostginger.com/famous-movie-locations/'

titles_ = []
r = requests.get(url)
soup = BeautifulSoup(r.content, 'lxml')
titles = soup.select('h3')
for t in titles:
    tt = t.text.strip()
    titles_.append(tt)
titles_

输出:

['Café des Deux Moulins as seen in Amélie (2001)',
 'Royal Palace of Caserta as seen in Angels and Demons (2009)',
 'Odesa Steps as seen in Battleship Potemkin (1926)',
 'Promenade Plantée as seen in Before Sunset (2004)',
 'Curracloe\xa0Beach as seen in Brooklyn (2015)',
 'Belfry of Bruges as seen in In Bruges (2008)',
 'Sirmione as seen in Call Me By Your Name (2017)',
 'Villa del Balbianello as seen in Casino Royale (2006)',
 'Neuschwanstein Castle as seen in Chitty Chitty Bang Bang (1968)',
 'Nyhavn Harbour as seen in The Danish Girl (2015)',
 'Rosslyn Chapel as seen in The Da Vinci Code (2006)',
 'Highclere Castle as seen in Downton Abbey (2010-2019)',
 'Juvet Landscape Hotel as seen in Ex Machina (2014)',
 'Mini Hollywood as seen in For a Few Dollars More (1965)',
 'The Dark Hedges as seen in Game of Thrones (2011-2019)',
 'Kaufhaus Görlitz as seen in The Grand Budapest Hotel (2014)',
 'Bar Vitelli as seen in The Godfather (1972)',
 'Glenfinnan Viaduct as seen in Harry Potter and the Chamber of Secrets (2002)',
 'Old Royal Naval College as seen in The King’s Speech (2010)',
 'Trevi Fountain as seen in La Dolce Vita (1960)',
 'Juliet’s House as seen in Letters to Juliet (2010)',
 'Church of Agios Ioannis Kastri as seen in Mamma Mia! (2008)',
 'Palace of Versailles as seen in Marie Antoinette (2006)',
 'Shakespeare & Company Bookshop as seen in Midnight in Paris (2011)',
 'Doune Castle as seen in Monty Python and The Holy Grail (1975)',
 'The Notting Hill Bookshop as seen in Notting Hill (1999)',
 'Belchite as seen in Pan’s Labyrinth (2006)',
 'Umschlagplatz as seen in The Pianist (2002)',
 'Popeye Village as seen in Popeye (1980)',
 'Cliffs of Moher as seen in The Princess Bride (1987)',
 'Wicklow Mountains in P.S. I Love You (2007)',
 'Mouth of Truth as seen in Roman Holiday (1953)',
 'Piłsudskiego Bridge as seen in Schindler’s List (1993)',
 'Kirkjufell Mountain as seen in The Secret Life of Walter Mitty (2013)',
 'Residenzplatz as seen in The Sound of Music (1965)',
 'The Fairy Glen as seen in Stardust (2007)',
 'Skellig Michael as seen in Star Wars Episode VIII: The Last Jedi (2017)',
 'Spanish Steps as seen in The Talented Mr Ripley (1999)',
 'Riesenrad Ferris Wheel as seen in The Third Man (1949)',
 'Hotel Carlton as seen in To Catch a Thief (1955)',
 'Tibidabo Amusement Park as seen in Vicky Cristina Barcelona (2008)',
 'Haweswater Reservoir as seen in Withnail & I (1989)',
 'Aït Benhaddou as seen in Gladiator (2000)',
 'Masai Mara as seen in Out of Africa (1985)',
 'Sidi Idriss Hotel as seen in Star Wars Episode III: A New Hope (1977)',
 'Maya Bay as seen in The Beach (2000)',
 'Hongcun Ancient Village as seen in Crouching Tiger Hidden Dragon (2000)',
 'Lebua State Tower as seen in The Hangover Part II (2011)',
 'Petra as seen in Indiana Jones and the Last Crusade (1989)',
 'Angkor Thom as seen in Lara Croft: Tomb Raider (2001)',
 'Park Hyatt Hotel as seen in Lost in Translation (2003)',
 'Phang Nga Bay as seen in The Man with the Golden Gun (1974)',
 'Burj Khalifa as seen in Mission: Impossible – Ghost Protocol (2011)',
 'Chhatrapati Shivaji Maharaj Terminus as seen in Slumdog Millionaire (2008)',
 'King’s Canyon as seen in The Adventures of Priscilla, Queen of the Desert (1994)',
 'Hobbiton as seen in The Lord of the Rings: The Fellowship of the Ring (2001)',
 'Pine Oak Court as seen in Neighbours (1985-Present)',
 'Devil’s Tower as seen in Close Encounters of the Third Kind (1977)',
 'Route 66 as seen in Easy Rider (1969)',
 'Art Institute of Chicago as seen in Ferris Bueller’s Day Off (1986)',
 'Monument Valley as seen in Forrest Gump (1994)',
 'New York Public Library as seen in Ghostbusters (1984)',
 'Salvation Mountain as seen in Into the Wild (2007)',
 'Martha’s Vineyard as seen in Jaws (1975)',
 'Griffith Observatory as seen in Rebel Without a Cause (1955)',
 'Philadelphia Museum of Art as seen in Rocky (1976)',
 'Edmund Pettus Bridge as seen in Selma (2014)',
 'Timberline Lodge as seen in The Shining (1980)',
 'Dead Horse Point State Park as seen in Thelma & Louise (1991)',
 'Golden Gate Bridge as seen in Vertigo (1958)',
 'Katz’s Delicatessen as seen in When Harry Met Sally (1989)',
 'Prairie Mountain as seen in Brokeback Mountain (2005)',
 'Iguazu Falls as seen in Indiana Jones and the Crystal Skull (2008)',
 'Machu Picchu as seen in The Motorcycle Diaries (2004)',
 'Bahia De Cacaluta Beach as seen in Y Tu Mamá También (2001)',
 '3 thoughts on “75+ Famous Movie Locations You Can Actually Visit”',
 'Leave a Reply Cancel reply']

这是一个很好的列表理解用例:

import requests
from bs4 import BeautifulSoup as BS

with requests.Session() as session:
    r = session.get('https://almostginger.com/famous-movie-locations/')
    r.raise_for_status()
    soup = BS(r.text, 'lxml')
    titles = [title.text+'\n' for title in soup.select('h3')]
    print(*titles)

相关问题 更多 >

    热门问题