爬取的資料本身有空格,怎么洗掉這些空格?求大佬們給個解決方法,網上的. replace()也已經試過了
import requests
from bs4 import BeautifulSoup
url = 'https://xingtai.anjuke.com/sale/'
# 身份證
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0'}
resp = requests.get(url, headers=headers)
# print(resp.text) # d列印網頁內容 為文本形式
# 下面是出現亂碼時解碼用的
# print(resp.content。decode('utf-8')) # 列印網頁內容 為二進制形式
##以上就是獲取了網頁內容##
##下面來轉換網頁##
html = resp.text
soup = BeautifulSoup(html, 'html.parser')
# print(soup)
# 進行資料提取
infos = soup.find('ul',{'id': 'houselist-mod-new'},{'class': 'list-item'}).find_all('li')
#進行查找 查找一個用find 所有的時候用find_all ####ul后面跟上屬性
print(infos)
for info in infos:
name = info.find('div', {'class': 'house-details'}).find('a',{'class': 'houseListTitle'}).get_text()
newname = name.lstrip()
print(newname)
price = info.find('div', {'class': 'pro-price'}).find('span',{'class': 'price-det'}).get_text()
print(price)
postion = info.find('span',{'class': 'comm-address'}).get_text()
newpostion = postion.lstrip()
newpostion = newpostion.replace('n', '')
#newpostion = postion.rstrip()
print(newpostion)

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/86543.html
標籤:其他技術專區
上一篇:哪里有WOW腳本學習平臺
下一篇:移動硬碟病理分析
