Re: String-Operation
Verfasst: Mittwoch 21. August 2019, 06:26
Eine gute Quelle für Tips und Tricks rund um Pandas bietet Kevin Markham auf seinem Youtube Channel https://www.youtube.com/user/dataschool/videos und auf Twitter.
Seit 2002 Diskussionen rund um die Programmiersprache Python
https://www.python-forum.de/
Ich würde deinen Code aber gerne weiterverwenden, da er wohl kürzer und besser ist.Zeile 32: TypeError: list indices must be integers or slices, not str
Code: Alles auswählen
<tr class='r'>
<td class='TRr'>
<a href='http://www.openstreetmap.org/?mlon=9.4910&mlat=51.2993&zoom=6' target='_blank'><img src='img/flags/de.gif' class='flag' width='18px' title='//DE' alt='DE' border='0'></a>
<a href='router_detail.php?FP=749ef4a434dfd00dab31e93de86233fb916d31e3' target='_blank'>ExitNinja</a></td>
<td class='TDb'>
<table cellspacing='0' cellpadding='0' class='bwb'>
<tr title='57259 KBs'><td class='bwr5'><img src='img/bar/9.png' width='12px' height='15px' alt='57259'></td><td> <small> 57259</small></td></tr>
</table>
</td>
<td class='TDcb'>33 d</td>
<td class='TDS'>
<table class='iT'>
<tr><td class='iT'>46.165.245.154 [<a class='who' href='/cgi-bin/whois.pl?ip=46.165.245.154' target='_blank'>46.165.245.154</a>]</td>
<td><img src='img/status/Fast.png' title='Fast Server' alt='Fast Server'></td>
<td><img src='img/status/Exit.png' title='Exit Server' alt='Exit Server'></td>
<td><img src='img/status/Dir.png' title='Directory Server' alt='Directory Server'></td>
<td><img src='img/status/Guard.png' title='Guard Server' alt='Guard Server'></td>
<td><img src='img/status/Stable.png' title='Stable Server' alt='Stable Server'></td>
<td><img src='img/os-icons/Linux.png' title='Tor 0.4.0.5 on Linux' alt='Tor 0.4.0.5 on Linux'></td>
</tr>
</table>
</td>
<td class='TDc'><b>443</b></td>
<td class='TDc'><b>80</b></td>
<td class='F0'></td>
<td class='TDS'>2014-11-06 </td>
<td class='TDS'>LEASEWEB-DE-FRA-10, DE </td>
<td class='TDS'>28753 </td>
<td class='TDc'>120000 </td>
<td class='TDS'> </td>
</tr>
Code: Alles auswählen
result_rounter_name = re.search(r"target[=]*'[_]*blank'>[a-zA-Z0-9]{3,30}<[/]*a><[/]*td>", table_row)
Code: Alles auswählen
from bs4 import BeautifulSoup
from io import StringIO
import re
TEST_STREAM = StringIO("""\
<tr class='r'>
<td class='TRr'>
<a href='http://www.openstreetmap.org/?mlon=9.4910&mlat=51.2993&zoom=6' target='_blank'><img src='img/flags/de.gif' class='flag' width='18px' title='//DE' alt='DE' border='0'></a>
<a href='router_detail.php?FP=749ef4a434dfd00dab31e93de86233fb916d31e3' target='_blank'>ExitNinja</a></td>
<td class='TDb'>
<table cellspacing='0' cellpadding='0' class='bwb'>
<tr title='57259 KBs'><td class='bwr5'><img src='img/bar/9.png' width='12px' height='15px' alt='57259'></td><td> <small> 57259</small></td></tr>
</table>
</td>
<td class='TDcb'>33 d</td>
<td class='TDS'>
<table class='iT'>
<tr><td class='iT'>46.165.245.154 [<a class='who' href='/cgi-bin/whois.pl?ip=46.165.245.154' target='_blank'>46.165.245.154</a>]</td>
<td><img src='img/status/Fast.png' title='Fast Server' alt='Fast Server'></td>
<td><img src='img/status/Exit.png' title='Exit Server' alt='Exit Server'></td>
<td><img src='img/status/Dir.png' title='Directory Server' alt='Directory Server'></td>
<td><img src='img/status/Guard.png' title='Guard Server' alt='Guard Server'></td>
<td><img src='img/status/Stable.png' title='Stable Server' alt='Stable Server'></td>
<td><img src='img/os-icons/Linux.png' title='Tor 0.4.0.5 on Linux' alt='Tor 0.4.0.5 on Linux'></td>
</tr>
</table>
</td>
<td class='TDc'><b>443</b></td>
<td class='TDc'><b>80</b></td>
<td class='F0'></td>
<td class='TDS'>2014-11-06 </td>
<td class='TDS'>LEASEWEB-DE-FRA-10, DE </td>
<td class='TDS'>28753 </td>
<td class='TDc'>120000 </td>
<td class='TDS'> </td>
</tr>""")
def get_router_name(soup):
pattern = re.compile(r'router_detail.*')
match = soup.find('a', href=pattern)
if not match:
raise ValueError('Missing router_detail link')
return match.text
def main():
soup = BeautifulSoup(TEST_STREAM)
print(get_router_name(soup))
if __name__ == '__main__':
main()
Allerdings. Ich hatte halt noch in Erinnerung, dass BeautifulSoup reguläre Ausdrücke beherrscht, aber ich wusste nicht mehr genau, wie man vorgehen muss. Also Google angeschmissen und nach "beautifulsoup href regex" gesucht und dann war der Rest schnell runtergetippt und funktionierte auch gleich beim ersten Durchlauf.Strawk hat geschrieben: Donnerstag 22. August 2019, 14:21 Es ist nunmal des einen Spaziergang des anderen Gewaltmarsch
generiert auch tatsächlich das Skript results.txt. Dessen einziger Eintrag ist jedochpython -m line_profiler scrapeTor.py.lprof > results.txt
.Timer unit: 3.20721e-07 s
dann:kernprof -l scrapeTor.py
Mein Skript heißt scrapeTor.pypython -m line_profiler scrapeTor.py.lprof > results.txt
It has been written to be used as a decorator, so in your script, you decorate the functions you want to profile with @profile.Code: Alles auswählen
@profile def slow_function(a, b, c):