在HTML中使用BeautifulSoup查找元素

时间:2018-01-30 15:48:03

标签: python html beautifulsoup

我需要在html代码中找到<td alert="0" op="0" class=" es_numero cell_imps24ad"><span>1.204</span></td>的所有元素。我无法发送所有的HTML代码,因为它是机密信息。

我正在尝试使用此代码:

# encoding=utf8
# -*- coding: utf-8 -*-
import random
import requests
from requests.auth import HTTPBasicAuth
import sys
import csv
from bs4 import BeautifulSoup


reload(sys)
sys.setdefaultencoding('utf-8')
lista = []
number = str(random.random())

user = ''
passwd = ''
url = ''
login = requests.get(url, auth=HTTPBasicAuth(user, passwd))
url_sitios = ''

sitios = requests.get(url_sitios, auth=HTTPBasicAuth(user, passwd))
sitios2 = sitios.text
html = sitios2
soup = BeautifulSoup(html)

for item in soup.find_all("td", {"class": " es_numero cell_imps24ad"}):
    print item.text, item.next_sibling

我想要的输出是这样的: es_numero cell_imps24ad : 1.204

1 个答案:

答案 0 :(得分:0)

您需要将解析类型传递给BeautifulSoup

soup = BeautifulSoup(html, 'lxml') #add the 'lxml' parser
for item in soup.find_all("td", {"class": " es_numero cell_imps24ad"}):
   print item.text, item.next_sibling

修改:给定nombreurl标记的html,您可以试试这个:

from bs4 import BeautifulSoup as soup
import re
s = "<url>https://www.google.com.ar/</url>\n<nombre>google.com.ar‌​</nombre>"
data = map(lambda x:x.text, soup(s, 'lxml').find_all(re.compile('nombre|url')))

输出:

[u'https://www.google.com.ar/', u'google.com.ar\u200c\u200b']

编辑2:用于较小的提取:

from bs4 import BeautifulSoup as soup
s = '<ultimas24hrs> <item id="imps24ad">0</item>'
new_s = soup(s, 'lxml')
the_id = int(new_s.find('item', {'id':"imps24ad"}).text)