簡體   English   中英

使用Beautiful Soup在python html解析中使用xml數據的理想方法是什么?

[英]What is the ideal way to use xml data in python html parsing with Beautiful Soup?

使用Beautiful Soup在python html解析中將xml轉換為文本的理想方法是什么?

當我使用Python 2.7 BeautifulSoup庫進行html解析時,可以進入“湯”步驟,但是我不知道如何提取所需的數據,因此我嘗試將它們全部轉換為字符串。

在下面的示例中,我想提取span標記中的所有數字並將它們加起來。 有沒有更好的辦法?

XML數據: http //python-data.dr-chuck.net/comments_324255.html

碼:

import urllib2
from BeautifulSoup import *
import re

url = 'http://python-data.dr-chuck.net/comments_324255.html'
html = urllib2.urlopen(url).read()
soup = BeautifulSoup(html)
spans = soup('span')
lis = list()
span_str = str(spans)
sp = re.findall('([0-9]+)', span_str)
count = 0
for i in sp:
    count = count + int(i)
print('Sum:', count)

不需要正則表達式:

from bs4 import BeautifulSoup
from requests import get

url = 'http://python-data.dr-chuck.net/comments_324255.html'
html = get(url).text
soup = BeautifulSoup(html, 'lxml')

count = sum(int(n.text) for n in soup.findAll('span'))
import requests, bs4
r = requests.get("http://python-data.dr-chuck.net/comments_324255.html")
soup = bs4.BeautifulSoup(r.text, 'lxml')

sum(int(span.text) for span in soup.find_all(class_="comments"))

輸出:

2788

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM