Очистка данных при парсинге с помощью Beautiful soup - PullRequest
0 голосов
/ 03 августа 2020
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'

Чтобы получить "Турция" в качестве вывода, я могу all[1].find('h4').find("span").text.replace(" ", "").replace("(","").replace(")","") Есть ли лучший способ написать этот код и что еще более важно, как получить на выходе только «Помидоры»?

<h4>
      " Tomatoes "                
      <span>(Turkey)</span>
</h4>

Ответы [ 2 ]

2 голосов
/ 03 августа 2020

Это односторонний:

import requests
from bs4 import BeautifulSoup
countries = []
vegetables = []
remove = ['(', ')']
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")
text = ''
all = soup.select("div.col-sm-3.nrtc-p-10 h4")

# Vegetables
print('Vegetables:\n')
for vegetable in all:
    print(vegetable.find(text=True, recursive=False).strip())
    vegetables.append(vegetable.find(text=True, recursive=False).strip())


# Countries:
print('\n\nCountries:\n')

for span in all:
    for t in span.find('span').get_text(strip=True):
        if not t in remove:
            text += t
    print(text)
    countries.append(text)
    text= ''


# Vegetables and Countries
for v, c in zip(vegetables, countries):
    print(f'{v} - {c}')

распечатывает:

Vegetables:

Tapioca
Tomatoes
Rosemary
Beef Tomatoes
Red Cherry Tomatoes
Red Cherry Tomatoes (Vine)
Yellow Cherry Tomatoes
Plum Tomatoes
Plum Cherry Tomatoes
Vine Tomatoes
....

Countries:

Srilanka
Turkey
Kenya
Holland
Netherland
Netherland
Netherland
Netherland
Holland
Netherland
....


Tapioca - Srilanka
Tomatoes - Turkey
Rosemary - Kenya
Beef Tomatoes - Holland
Red Cherry Tomatoes - Netherland
Red Cherry Tomatoes (Vine) - Netherland
Yellow Cherry Tomatoes - Netherland
Plum Tomatoes - Netherland
Plum Cherry Tomatoes - Holland
Vine Tomatoes - Netherland
Turnip - Iran
Baby Turnip - South Africa
Yams (Suran) - India
Green Baby Zucchini - South Africa
....

Примечание. Я сократил напечатанный здесь текст ..

Этот метод особенно хорош, если есть много разных символов, которые не принимаются

1 голос
/ 03 августа 2020

Насколько я понимаю, вы ищете только название овоща, а не страну. Если вы хотите избавиться от названия страны, вы можете сделать следующее:

# Delete the country spans
for span in soup.select("div.col-sm-3.nrtc-p-10 h4 span"):
    span.extract()

# Get a list of all the vegetables
veg_list = [h4.text.strip() for h4 in soup.select("div.col-sm-3.nrtc-p-10 h4")]
print(veg_list)

Tapioca
Tomatoes
Rosemary
Beef Tomatoes
Red Cherry Tomatoes
...
...