bs4中的get_text()对于跨度标签是否有所不同?无法删除跨度标签

时间:2019-06-05 19:54:26

标签: python html web-scraping beautifulsoup

在制作网络刮板时,我能够找到可用的数据。 在2个数据字段上,我能够使用beautifulsoup get_text()从数据中删除html

但是当我使用get_text()时,第三个字段将不起作用。我可以使用它来给我整个span标签,而不是其中的文本。

我已经尝试过不同的迭代来获得相同的数据,它将为我提供整个span标签。东西

试图将总线号设置为该span标签内的电话号码

<span class="business--telephoneNumber" itemprop="telephone">01430 422826 </span>

我尝试过

from bs4 import BeautifulSoup
import requests
import csv

data_list=[]
url = 'https://www.yell.com/ucs/UcsSearchAction.do?keywords=farmer&location=leeds'
headers = {
        'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
    }
site = requests.get(url, headers=headers, timeout=5);
if site.status_code is 200:
    content = BeautifulSoup(site.content, 'html.parser')
    #print(content)
    questions = content.find_all(class_='businessCapsule') 
for question in questions:
        busname = question.find(class_='businessCapsule--name').get_text()
        bustype =   question.find(class_='businessCapsule--classification').get_text()
        busnum = question.find('span', {'itemprop': 'telephone'})
        print(busnum)
        busnumber = busnum.get_text()
        new_data = {"busname": busname, "bustype": bustype, "busnumber": busnumber}
        data_list.append(new_data)
    with open ('selector.csv','w') as file:
        writer = csv.DictWriter(file, fieldnames = ["busname", "bustype", "busnumber"], delimiter = ';')
        writer.writeheader()
        for row in data_list:
            writer.writerow(row)

以及

from bs4 import BeautifulSoup
import requests
import csv

data_list=[]
url = 'https://www.yell.com/ucs/UcsSearchAction.do?keywords=farmer&location=leeds'
headers = {
        'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
    }
site = requests.get(url, headers=headers, timeout=5);
if site.status_code is 200:
    content = BeautifulSoup(site.content, 'html.parser')
    #print(content)
    questions = content.find_all(class_='businessCapsule')
    for question in questions:
        busname = question.find(class_='businessCapsule--name').get_text()
        bustype =   question.find(class_='businessCapsule--classification').get_text()
        busnumber = question.find('span', {'itemprop': 'telephone'}).get_text()
        new_data = {"busname": busname, "bustype": bustype, "busnumber": busnumber}
        data_list.append(new_data)
    with open ('selector.csv','w') as file:
        writer = csv.DictWriter(file, fieldnames = ["busname", "bustype", "busnumber"], delimiter = ';')
        writer.writeheader()
        for row in data_list:
            writer.writerow(row)

在两种情况下,get_text()都会出现此错误

Traceback (most recent call last):
  File "webscraper2.py", line 22, in <module>
    busnumber = busnum.get_text()
AttributeError: 'NoneType' object has no attribute 'get_text'

如果删除了get_text,它将给出整个标签

<span class="business--telephoneNumber" itemprop="telephone">01430 422826 </span>

我只需要被保险人的电话号码。

更新-最新代码

from bs4 import BeautifulSoup as bs
import requests
import csv

data_list=[]
url = 'https://www.yell.com/ucs/UcsSearchAction.do?keywords=farmer&location=leeds'
headers = {
        'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
    }
site = requests.get(url, headers=headers, timeout=5)
soup = bs(site.content, 'html.parser')
questions = soup.select('.businessCapsule--mainContent')
for question in questions:
    busname = question.find(class_='businessCapsule--name').get_text()
    bustype =   question.find(class_='businessCapsule--classification').get_text()
    busnumber = question.select_one('span.business--telephoneNumber').text
    print(busnumber)
    new_data = {"busname": busname, "bustype": bustype, "busnumber": busnumber}
    data_list.append(new_data)
with open ('selector.csv','w') as file:
    writer = csv.DictWriter(file, fieldnames = ["busname", "bustype", "busnumber"], delimiter = ';')
    writer.writeheader()
    for row in data_list:
        writer.writerow(row)

1 个答案:

答案 0 :(得分:1)

您需要获得其他父母才能选择合适的孩子,并为孩子更改选择器,如下所示:

import requests
from bs4 import BeautifulSoup as bs

url = 'https://www.yell.com/ucs/UcsSearchAction.do?keywords=farmer&location=leeds'
headers = {
        'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
    }
site = requests.get(url, headers=headers, timeout=5)
soup = bs(site.content, 'lxml')
questions = soup.select('.businessCapsule--mainContent:has(span.business--telephoneNumber)')
for question in questions:
    print(question.select_one('span.business--telephoneNumber').text)

如果您选中此不同的父选择器,您会看到它选择了带有信息的整个框,这样您就可以选择各个孩子了

enter image description here

如果过于严格,可以测试电话是否存在

import requests
from bs4 import BeautifulSoup as bs

url = 'https://www.yell.com/ucs/UcsSearchAction.do?keywords=farmer&location=leeds'
headers = {
        'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
    }
site = requests.get(url, headers=headers, timeout=5)
soup = bs(site.content, 'lxml')
questions = soup.select('.businessCapsule--mainContent')
for question in questions:
    tel = question.select_one('span.business--telephoneNumber')
    if tel is None:
        tel = 'Not present'
    else:
        tel = tel.text
    print(tel)