2016-06-23 15 views
1

Ich suche BeautifulSoup4 und entweder urllib oder Anfragen zu verwenden, um einige Dokumente zu kratzen, aber einige von ihnen haben & nbsp in sie statt normaler Räume. In einigen der Dokumente gibt es Zeilen wie folgt aus:BeautifulSoup4 Konvertieren von HTML-Entitäten (non-breaking Raum?), Bevor die Suche (Python 3.X)

"Pursuant to&nbspthe requirements&nbspof Section&nbsp13..." 

aber sie können überall platziert werden, so dass sie durch Leerzeichen, ex ersetzt werden müssen:

"Pursuant to the requirements of Section 13...." 

Ich brauche sie zu ersetzen bevor ich den soup.find tun(), so etwas wie diese:

#code to get request from server goes here 

soup=BeautifulSoup(requestedPage) 
#remove nbsp 
sectionRequired=soup.find(text=re.compile("Pursuant to the requirements of Section 13")) 

Wie bin ich in der Lage, die & nbsp Räume mit normalen Räumen in einer Web-Seite zu ersetzen, bevor ich die BS4 Funktion finden verwenden, also kann ich ein string match in der soup.find() machen?

Antwort

1

können Sie html.parser.unescape() verwenden:

import html.parser as hp 
import re 

data = response.text 
soup = BeautifulSoup(hp.unescape(data), "lxml") 

p = soup.find(text=re.compile("Pursuant to the requirements of Section 13")) 
print(p) 

Warum Sie Text nicht ist es ein Newline auch ist, die folgenden regex Werke:

p = soup.find(text=re.compile("Pursuant\s+to\s+the\s+requirements\s+of\s+Section\s+13")) 

Laufen auf:

In [15]: url = "https://www.sec.gov/Archives/edgar/data/1800/000110465907013496/a07-1583_110k.htm" 

In [16]: response = requests.get(url, headers={ 
    ....:  "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36" 
    ....: }) 

In [17]: data = response.text 

In [18]: soup = BeautifulSoup(hp.unescape(data), "lxml") 

In [19]: p = soup.find(text=re.compile("Pursuant\s+to\s+the\s+requirements\s+of\s+Section\s+13")) 

In [20]: print(p) 
Pursuant to the 
requirements of Section 13 or 15(d) of the Securities Exchange Act of 
1934, Abbott Laboratories has duly caused this report to be signed on its 
behalf by the undersigned, thereunto duly authorized. 

Sie sehen, nach Pursuant to the gibt es eine neue Zeile, so dass Ihr Text nicht übereinstimmen konnte. Die Verwendung von \s+ funktioniert für alle Leerzeichen. Das Unescaping wird auch nicht wirklich benötigt, die Textübereinstimmung wird gut funktionieren.

die zweite Tabelle zu erhalten, nachdem:

table = p.find_next("table").find_next("table") 

Welche gibt Ihnen:

<table border="0" cellpadding="0" cellspacing="0" style="border-collapse:collapse;"> 
<tr style="page-break-inside:avoid;"> 
<td style="padding:0pt .7pt 0pt 0pt;width:210.0pt;" valign="top" width="280"> 
<p style="font-size:10.0pt;margin:0pt 0pt .0001pt;"><!-- SET mrlNoTableShading -->/s/ MILES D. 
    WHITE</p> 
</td> 
<td style="padding:0pt .7pt 0pt 0pt;width:30.0pt;" valign="top" width="40"> 
<p style="margin:0pt 0pt .0001pt;"><font face="Times New Roman" size="2" style="font-size:1.0pt;"> </font></p> 
</td> 
<td style="padding:0pt .7pt 0pt 0pt;width:215.7pt;" valign="top" width="288"> 
<p style="margin:0pt 0pt .0001pt;"><font face="Times New Roman" size="2" style="font-size:10.0pt;">/s/ ROXANNE S. 
    AUSTIN</font></p> 
</td> 
</tr> 
<tr style="page-break-inside:avoid;"> 
<td style="border:none;border-top:solid windowtext 1.0pt;padding:0pt .7pt 0pt 0pt;width:210.0pt;" valign="top" width="280"> 
<p style="font-size:10.0pt;margin:0pt 0pt .0001pt;"><!-- SET mrlHTMLTableFull --><!-- SET mrlNoTableShading -->Miles D. White<br/> 
    Chairman of the Board, Chief Executive<br/> 
    Officer and Director of Abbott Laboratories<br/> 
    (principal executive officer)</p> 
</td> 
<td style="padding:0pt .7pt 0pt 0pt;width:30.0pt;" valign="top" width="40"> 
<p style="margin:0pt 0pt .0001pt;"><font face="Times New Roman" size="2" style="font-size:1.0pt;"> </font></p> 
</td> 
<td style="border:none;border-top:solid windowtext 1.0pt;padding:0pt .7pt 0pt 0pt;width:215.7pt;" valign="top" width="288"> 
<p style="margin:0pt 0pt .0001pt;"><font face="Times New Roman" size="2" style="font-size:10.0pt;">Roxanne S. Austin<br/> 
    Director of Abbott Laboratories</font></p> 
</td> 
</tr> 
<tr style="page-break-inside:avoid;"> 
<td style="border:none;border-bottom:solid windowtext 1.0pt;padding:0pt .7pt 0pt 0pt;width:210.0pt;" valign="top" width="280"> 
<p style="margin:24.0pt 0pt .0001pt;"><font face="Times New Roman" size="2" style="font-size:10.0pt;">/s/ RICHARD A. GONZALEZ</font></p> 
</td> 
<td style="padding:0pt .7pt 0pt 0pt;width:30.0pt;" valign="top" width="40"> 
<p style="margin:24.0pt 0pt .0001pt;"><font face="Times New Roman" size="2" style="font-size:1.0pt;"> </font></p> 
</td> 
<td style="border:none;border-bottom:solid windowtext 1.0pt;padding:0pt .7pt 0pt 0pt;width:215.7pt;" valign="top" width="288"> 
<p style="margin:24.0pt 0pt .0001pt;"><font face="Times New Roman" size="2" style="font-size:10.0pt;">/s/ WILLIAM M. DALEY</font></p> 
</td> 
</tr> 
<tr style="page-break-inside:avoid;"> 
<td style="border:none;padding:0pt .7pt 0pt 0pt;width:210.0pt;" valign="top" width="280"> 
<p style="margin:0pt 0pt .0001pt;"><font face="Times New Roman" size="2" style="font-size:10.0pt;">Richard A. Gonzalez<br/> 
    President and Chief Operating Officer<br/> 
    and Director of Abbott Laboratories</font></p> 
</td> 
<td style="padding:0pt .7pt 0pt 0pt;width:30.0pt;" valign="top" width="40"> 
<p style="margin:0pt 0pt .0001pt;"><font face="Times New Roman" size="2" style="font-size:1.0pt;"> </font></p> 
</td> 
<td style="border:none;padding:0pt .7pt 0pt 0pt;width:215.7pt;" valign="top" width="288"> 
<p style="margin:0pt 0pt .0001pt;"><font face="Times New Roman" size="2" style="font-size:10.0pt;">William M. Daley<br/> 
    Director of Abbott Laboratories</font></p> 
</td> 
</tr> 
<tr style="page-break-inside:avoid;"> 
<td style="border:none;border-bottom:solid windowtext 1.0pt;padding:0pt .7pt 0pt 0pt;width:210.0pt;" valign="top" width="280"> 
<p style="margin:24.0pt 0pt .0001pt;"><font face="Times New Roman" size="2" style="font-size:10.0pt;">/s/ THOMAS C. FREYMAN</font></p> 
</td> 
<td style="padding:0pt .7pt 0pt 0pt;width:30.0pt;" valign="top" width="40"> 
<p style="margin:0pt 0pt .0001pt;"><font face="Times New Roman" size="2" style="font-size:1.0pt;"> </font></p> 
</td> 
<td style="border:none;border-bottom:solid windowtext 1.0pt;padding:0pt .7pt 0pt 0pt;width:215.7pt;" valign="bottom" width="288"> 
<p style="margin:0pt 0pt .0001pt;"><font face="Times New Roman" size="2" style="font-size:10.0pt;">/s/ W. 
    JAMES FARRELL</font></p> 
</td> 
</tr> 
<tr style="page-break-inside:avoid;"> 
<td style="border:none;padding:0pt .7pt 0pt 0pt;width:210.0pt;" valign="top" width="280"> 
<p style="margin:0pt 0pt .0001pt;"><font face="Times New Roman" size="2" style="font-size:10.0pt;">Thomas C. Freyman<br/> 
    Executive Vice President, Finance<br/> 
    and Chief Financial Officer<br/> 
    (principal financial officer)</font></p> 
</td> 
<td style="padding:0pt .7pt 0pt 0pt;width:30.0pt;" valign="top" width="40"> 
<p style="margin:0pt 0pt .0001pt;"><font face="Times New Roman" size="2" style="font-size:1.0pt;"> </font></p> 
</td> 
<td style="border:none;padding:0pt .7pt 0pt 0pt;width:215.7pt;" valign="top" width="288"> 
<p style="margin:0pt 0pt .0001pt;"><font face="Times New Roman" size="2" style="font-size:10.0pt;">W. James Farrell<br/> 
    Director of Abbott Laboratories</font></p> 
</td> 
</tr> 
<tr style="page-break-inside:avoid;"> 
<td style="border:none;border-bottom:solid windowtext 1.0pt;padding:0pt .7pt 0pt 0pt;width:210.0pt;" valign="top" width="280"> 
<p style="margin:24.0pt 0pt .0001pt;"><font face="Times New Roman" size="2" style="font-size:10.0pt;">/s/ GREG W. LINDER</font></p> 
</td> 
<td style="padding:0pt .7pt 0pt 0pt;width:30.0pt;" valign="top" width="40"> 
<p style="margin:0pt 0pt .0001pt;"><font face="Times New Roman" size="2" style="font-size:1.0pt;"> </font></p> 
</td> 
<td style="border:none;border-bottom:solid windowtext 1.0pt;padding:0pt .7pt 0pt 0pt;width:215.7pt;" valign="bottom" width="288"> 
<p style="margin:0pt 0pt .0001pt;"><font face="Times New Roman" size="2" style="font-size:10.0pt;">/s/ H. 
    LAURANCE FULLER</font></p> 
</td> 
</tr> 
<tr style="page-break-inside:avoid;"> 
<td style="border:none;padding:0pt .7pt 0pt 0pt;width:210.0pt;" valign="top" width="280"> 
<p style="margin:0pt 0pt .0001pt;"><font face="Times New Roman" size="2" style="font-size:10.0pt;">Greg W. Linder<br/> 
    Vice President and Controller<br/> 
    (principal accounting officer)</font></p> 
</td> 
<td style="padding:0pt .7pt 0pt 0pt;width:30.0pt;" valign="top" width="40"> 
<p style="margin:0pt 0pt .0001pt;"><font face="Times New Roman" size="2" style="font-size:1.0pt;"> </font></p> 
</td> 
<td style="border:none;padding:0pt .7pt 0pt 0pt;width:215.7pt;" valign="top" width="288"> 
<p style="margin:0pt 0pt .0001pt;"><font face="Times New Roman" size="2" style="font-size:10.0pt;">H. Laurance Fuller<br/> 
    Director of Abbott Laboratories</font></p> 
</td> 
</tr> 
</table> 
+0

Ich habe Ihre fix umgesetzt, aber wenn ich den nächsten Schritt zu tun, erhalte ich eine Typeerror: ‚NoneType 'Objekt ist nicht iterierbar. Dies bedeutet, dass der Fund nicht funktioniert hat, also ist immer noch etwas nicht in Ordnung. Wenn Sie den Link verwenden möchten, den ich verwende, hier ist der Link: https://www.sec.gov/Archives/edgar/data/1800/000110465907013496/a07-1583_110k.htm – Retroflux

+1

@ Retroflux, siehe die Bearbeitung –

+0

Richtig, also wird sich die Regex mit den Zeilenumbrüchen beschäftigen und die Unescoe mit den Unicode-Zeichen umgehen? – Retroflux