【发布时间】:2020-11-23 06:22:51
【问题描述】:
import requests, re
from bs4 import BeautifulSoup
r = requests.get('https://www.nrtcfresh.com/products/whole/vegetables-whole', headers={'User-agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:61.0) Gecko/20100101 Firefox/61.0'})
c = r.content
soup=BeautifulSoup(c,"html.parser")
#print(soup.prettify())
all = soup.find_all("div",{"class":"col-sm-3 nrtc-p-10"})
all[1].find("h4").text
输出如下
'\r\n Tomatoes\t\t\t\t (Turkey)\n'
要获得“Turkey”作为输出,我可以all[1].find('h4').find("span").text.replace(" ", "").replace("(","").replace(")","") 有没有更好的方法来编写这段代码,更重要的是,如何获得“Tomatoes”作为输出?
<h4>
" Tomatoes "
<span>(Turkey)</span>
</h4>
【问题讨论】:
标签: python pandas web-scraping beautifulsoup data-cleaning