Softwareentwicklung
›
Thread
Python und Beautifullsoup
S
Sebastian
Gast
28.09.2016 22:21
#4735973
Hi,
ich mache gerade meine ersten Versuche.
1 name = askopenfilename ( filetypes = (( "Import File" , "*.txt" ),( "All Files" , "*.*" )),
2 title = ""
3 )
4
5
6 f = codecs . open ( name , encoding = ' utf - 8 ' )
7
8
9
10
11 file_names = []
12 for file_name in f :
13 file_name = file_name . strip ()
14 if ',' in file_name :
15 for f in file_name . split ( ',' ) :
16 file_names . append ( f )
17 else :
18 file_names . append ( file_name )
19
20
21 workbook_overview = xlsxwriter . Workbook ( ' overview . xlsx ' )
22 worksheet_overview = workbook_overview . add_worksheet ( "overview" )
23 worksheet_overview . write ( ' A1 ' , ' url ' )
24 worksheet_overview . write ( ' B1 ' , ' titel ' )
25
26
27 count = 2
28
29 for file_name in file_names :
30 #try:
31 soup = BeautifulSoup ( urlopen ( file_name ), "html.parser" )
32 title = soup . find ( ' title ' )
33 print ( title )
34
35 worksheet_overview . write ( 'A' + str ( count ), ( file_name ))
36
37 worksheet_overview . write ( 'B' + str ( count ), ( title ))
38 count += 1
39
40
41 workbook_overview . close ()
Hier bekomme ich allerdings die Fehlermeldung:
1 TypeError: float () argument must be a string or a number , not ' method_descriptor '
2
3 During handling of the above exception , another exception occurred :
4
5 Traceback ( most recent call last ) :
6 File "D: \t esting.py" , line 72 , in < module >
7 worksheet_overview . write ( 'B' + str ( count ), ( title2 ))
Was mache ich denn falsch?
SR
S R
Gast
28.09.2016 22:24
#4735977
Vollständigen Code posten!
Woher soll ich wissen wo Zeile 72 ist?
SR
S R
Gast
28.09.2016 22:29
#4735982
Imports gehören auch dazu.
Wem es nicht aufgefallen ist:
worksheet_overview.write('B' + str(count), (title))
Und die Fehlermeldung bezieht sich auf:
worksheet_overview.write('B' + str(count), (title2))
KG
Kaj G.
(bloody)
(Firma: RUB)
28.09.2016 22:40
#4735992
S R schrieb:
> Und die Fehlermeldung bezieht sich auf:
Nein. Die erste Fehlermeldung sagt ganz klar:1 TypeError: float() argument must be a string or a number, not 'method_descriptor'
Erst danach kommen die anderen Fehler: 1 During handling of the above exception, another exception occurred:
Irgendwo wird ein falsches Argument übergeben, was float() nicht wandeln
kann. Dann gibt es eine Exception und das eine führt zum anderen.
Bitte poste den gesamten Code, sofern möglich. Alles andere ist
Kaffesatzlesen.
SR
S R
Gast
28.09.2016 22:44
#4735998
Das hab ich schon gesehen, wollte nur verdeutlichen dass es so schwierig
wird ;)
KG
Kaj G.
(bloody)
(Firma: RUB)
28.09.2016 23:12
#4736026
S R schrieb:
> Das hab ich schon gesehen, wollte nur verdeutlichen dass es so schwierig
> wird ;)
Ach so :D
Dann hab ich nichts gesagt ^.^
S
Sebastian
Gast
29.09.2016 07:29
#4736158
Hi,
danke schonmal für die Antworten.
Bis auf die Imports war das der gesamte Code.
1 # coding: utf-8
2
3
4 import codecs
5
6 import xlsxwriter
7
8 from bs4 import BeautifulSoup
9
10 from urllib.request import urlopen
11
12 from tkinter.filedialog import askopenfilename
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 name = askopenfilename(filetypes =(("Import File", "*.txt"),("All Files","*.*")),
31 title = ""
32 )
33
34
35 f = codecs.open(name, encoding='utf-8')
36
37
38
39
40
41
42 file_names = []
43 for file_name in f:
44 file_name = file_name.strip()
45 if ',' in file_name:
46 for f in file_name.split(','):
47 file_names.append(f)
48 else:
49 file_names.append(file_name)
50
51
52
53 workbook_overview = xlsxwriter.Workbook('overview.xlsx')
54 worksheet_overview = workbook_overview.add_worksheet("overview")
55 worksheet_overview.write('A1', 'url')
56 worksheet_overview.write('B1', 'titel')
57
58
59 count = 2
60
61 for file_name in file_names:
62
63 soup = BeautifulSoup(urlopen(file_name), "html.parser")
64 title = soup.find('title')
65 print (title)
66
67
68
69
70 worksheet_overview.write('A' + str(count), (file_name))
71
72 worksheet_overview.write('B' + str(count), (title))
73 count += 1
74
75
76
77
78
79
80
81 workbook_overview.close()
Meine Vermutung ist, dass es an dieser Zeile liegt
title = soup.find('title')
P
Planlos
Gast
29.09.2016 07:48
#4736174
Vermutung: "title" ist kein String, sondern ein HTML-Tag-Objekt. Schau
in der Suppen-Dokumentation nach, wie du da das "innerHMTL" oder
Text-Content herausbekommst.
Ich habe mir jetzt nicht alles angeschaut (habe nur die Formatierung
mal korrigiert), aber wenn du wirklich einmal 'titel' und einmal
'title' schreibst, scheint mir, dass genau dies dein Problem sein
könnte.
Antwort schreiben
Bitte melde dich an, um einen Beitrag zu schreiben.
Noch kein Account?
Die Registrierung ist kostenlos und dauert nur eine Minute.
Jetzt registrieren