Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 除此之外还有别的办法吗;剥离();及;替换();为了消除我刮取的数据中多余的空白?_Python_Html_Web Scraping_Strip - Fatal编程技术网

Python 除此之外还有别的办法吗;剥离();及;替换();为了消除我刮取的数据中多余的空白?

Python 除此之外还有别的办法吗;剥离();及;替换();为了消除我刮取的数据中多余的空白?,python,html,web-scraping,strip,Python,Html,Web Scraping,Strip,我对python非常陌生,我正在尝试建立一个webscraper,收集在《权力的游戏》中死去的角色的数据。我已经得到了我想要的数据,但我似乎无法从数据中获得一些额外的信息 我使用.replace(“,”)尝试了.strip()方法和.replace()方法,但每次都没有任何变化。下面是我的一段代码: url = "http://time.com/3924852/every-game-of-thrones-death/" r = requests.get(url) soup = Beautiful

我对python非常陌生,我正在尝试建立一个webscraper,收集在《权力的游戏》中死去的角色的数据。我已经得到了我想要的数据,但我似乎无法从数据中获得一些额外的信息

我使用
.replace(“,”)
尝试了
.strip()
方法和
.replace()
方法,但每次都没有任何变化。下面是我的一段代码:

url = "http://time.com/3924852/every-game-of-thrones-death/"
r = requests.get(url)
soup = BeautifulSoup(r.content, 'html.parser')

# Find the characters who have died by searching for the text embedded within the <div> tag with class = "headline"
find_deaths = soup.find_all('div', class_="headline")

# Strip out all the extra fluff at the beginning and end of the text and add it to list
for hit in find_deaths:
    deaths.append(hit.contents)
我尝试了以下方法,试图消除数据周围的多余绒毛,但它根本不会改变列表中的任何内容

for item in deaths:
       str(item).strip()


for item in deaths:
    str(item).replace("\n ", "")
使用上述两种方法中的任何一种,我认为它会从列表中的项目中去除所有多余的绒毛,但它似乎没有改变任何东西


除了剥离和替换之外,还有没有其他方法可以用来去除这些数据中的多余绒毛。

您应该使用列表理解:

deaths = [s.strip() for s in deaths]
但是,这里有很多不必要的中间步骤-您可以直接使用
find_all
中的列表理解:

deaths = [hit.contents[0].strip() for hit in soup.find_all('div', class_="headline")]
使用给定的网站和查询,
死亡

['Will', 'Jon Arryn', 'Jory Cassel', 'Benjen Stark', 'Robert Baratheon', 'Syrio Forel', 'Eddard Stark', 'Viserys Targaryen', 'Drogo', 'Rhaego', 'Mirri Maz Duur', 'Rakharo', 'Yoren', 'Renly Baratheon', 'Rodrik Cassel', 'Irri', 'Maester Luwin', 'Qhorin', 'Pyat Pree', 'Doreah', 'Xaro Xhoan Daxos', 'Hoster Tully', 'Jeor Mormont', 'Craster', 'Kraznys', 'Beric Dondarrion', 'Ros', 'Talisa Stark', 'Robb Stark', 'Catelyn Stark', 'Polliver', 'Tansy', 'Joffrey Baratheon', 'Karl Tanner', 'Locke', 'Rast', 'Lysa Arryn', 'Oberyn Martell', 'The Mountain', 'Grenn', 'Mag the Mighty', 'Pyp', 'Styr', 'Ygritte', 'Jojen Reed', 'Shae', 'Tywin Lannister', 'Mance Rayder', 'Janos Slynt', 'Barristan Selmy', 'Maester Aemon', 'Karsi', 'Shireen Baratheon', 'Hizdahr zo Loraq', 'Selyse Baratheon', 'Stannis Baratheon', 'Myranda', 'Meryn Trant', 'Myrcella Baratheon', 'Jon Snow', 'Areo Hotah', 'Doran Martell', 'Trystane Martell', 'The Flasher', 'Roose Bolton', 'Walda Bolton', 'Unnamed Bolton Child', 'Balon Greyjoy', 'Alliser Thorne', 'Olly', 'Ser Arthur Dayne', 'Osha', 'Khal Moro', 'Three-Eyed Raven', 'Leaf', 'Hodor', 'Aerys II Targaryen, "The Mad King"', 'Brother Ray', 'Lem', 'Brynden Tully (The Blackfish)', 'Lady Crane', 'The Waif', 'Razdal mo Eraz', 'Belicho Paenymion', 'Rickon Stark', 'Jon Umber', 'Wun Weg Wun Dar Wun', 'Ramsay Bolton', 'Grand Maester Pycelle', 'Lancel', 'The High Sparrow', 'Loras Tyrell', 'Mace Tyrell', 'Kevan Lannister', 'Margaery Tyrell', 'Tommen Baratheon', 'Walder Rivers', 'Lothar Frey', 'Walder Frey', 'Lyanna Stark', 'Nymeria Sand', 'Obara Sand', 'Tyene Sand', 'Olenna Tyrell', 'Randyll Tarly', 'Dickon Tarly', 'Thoros of Myr', 'Petyr "Littlefinger" Baelish', 'Ned Umber']

字符串是不可变的
strip()
replace()
返回新字符串,它们不会更改原始字符串

使用@Tomothy32建议的列表理解:

deaths = [hit.contents.strip() for hit in soup.find_all('div', class_="headline")]

由于我的位置原因,我无法进行测试,但是您应该能够避免这种情况,但是在元素的
name
属性中使用已清除的字符串,并使用class
anchor only

deaths = [item['name'] for item in soup.select('.anchor-only')]

strip()和replace()返回新字符串。正如@Michael Butscher指出的,您应该执行
new\u item=str(item)。strip
然后
new\u item
将在strip操作后成为
item
的副本
deaths = [item['name'] for item in soup.select('.anchor-only')]