1 回答
TA贡献1801条经验 获得超8个赞
您可以通过多种方式实现这一目标:
from bs4 import BeautifulSoup , NavigableString
import requests
main_url = "https://www.example.com/"
def getAndParseURL(url):
result = requests.get(url)
soup = BeautifulSoup(result.text, 'html.parser')
return(soup)
soup = getAndParseURL(main_url)
#ul = soup.select('ul[class="list-inline lot-breakdown-list"] li')
#for li in ul :
#x = li.find(text=True, recursive=False) # Will give you the text of the li skipping the text of child tag
#y = ' '.join([t for t in li.contents if type(t)== NavigableString]) # contents [<h5>Engine</h5>, '426/425 HP'] the text you want has a type of NavigableString and That's what we are returning .
ul = soup.select('ul[class="list-inline lot-breakdown-list"] li', recursive=True)
lis_e = []
for li in ul:
lis = []
lis.append(li.contents[1])
lis_e.extend(lis)
engine.append(lis_e[0])
trans.append(lis_e[1])
color.append(lis_e[2])
interior.append(lis_e[3])
scraped_data = pd.DataFrame({'engine': engine, 'transmission': trans, 'color': color, 'interior': interior})
scraped_data
- 1 回答
- 0 关注
- 89 浏览
添加回答
举报