0

I am creating a proxy scraper using reguler experessions. Html parsing with re is terrible, so I need to make sure no strings show up in the end result. How can I replace all strings with a space. The current code I had to clean up the parsed data was

print title.replace(',', '').replace("!", '').replace(":", '').replace(";", '').replace(str, '') 

The str portion was what I tried.... it did not work. Any other methods?

Ungifted
  • 5
  • 1
  • 4
    [Obligatory](http://stackoverflow.com/a/1732454/1223693). Use an HTML parsing library, such as BeautifulSoup. – tckmn Jan 04 '14 at 23:31
  • 1
    *“Html parsing with re is terrible”* – Regular expressions were never meant to parse HTML; and HTML isn’t designed to be parsable by regular expressions to begin with. – poke Jan 04 '14 at 23:32
  • 3
    The pony. He comes... – MattDMo Jan 04 '14 at 23:33
  • Try here: http://stackoverflow.com/questions/10017147/python-replace-characters-in-string – jump3r Jan 04 '14 at 23:34
  • *“make sure no strings show up”*, *“replace all strings”*, *“str portion was what I tried”* – What *strings* are you even referring to? How do you want to replace *all strings*? What *all strings*? If I replace all strings *in a string* with a space, I get just a space back. – poke Jan 04 '14 at 23:39
  • I have the sourceCode for the website. I want to keep all intergers but get rid of all strings – Ungifted Jan 04 '14 at 23:42
  • 3
    @Ungifted So you actually want to *extract numbers*? Which numbers do you care about? *All of them* or just those numbers that are displayed somewhere? (There could be numbers in HTML attributes for example, or somewhere else in the code). – poke Jan 04 '14 at 23:44
  • `print " "` - if you replace all strings with a space, whatever your definition of string is, then this is what you get after applying this replacement over and over again – Eric Jan 04 '14 at 23:46
  • I removed the tags... Now I got a mix of data. Words and numbers. I need only the numbers – Ungifted Jan 05 '14 at 00:54
  • @poke All the numbers present. Only numbers – Ungifted Jan 05 '14 at 01:11
  • @poke I want to do this just using reguler expressions – Ungifted Jan 05 '14 at 02:00
  • @Ungifted No, you don’t want to do that, for all the reasons me and others mentioned in the comments. – poke Jan 05 '14 at 02:10
  • @poke I cant install beautiful soup? I looked up tutorials and not one is working? I have a windows computer? – Ungifted Jan 05 '14 at 02:29

2 Answers2

3

If you want to extract all visible numbers from the HTML document, you can first use BeautifulSoup to parse the HTML document, and extract the text from it. And after that, you can extract all the numbers from those text elements:

from bs4 import BeautifulSoup
from urllib.request import urlopen
import re

# let’s use the StackOverflow homepage as an example
r = urlopen('http://stackoverflow.com')
soup = BeautifulSoup(r)

# As we don’t want to get the content from script related
# elements, remove those.
for script in soup(['script', 'noscript']):
    script.extract()

# And now extract the numbers using regular expressions from
# all text nodes we can find in the (remaining) document.
numbers = [n for t in soup(text=True) for n in re.findall('\d+', t)]

numbers will then contain all the numbers that were visible in the document. If you want to restrict the search to only certain elements, you can change the soup(text=True) part.

poke
  • 369,085
  • 72
  • 557
  • 602
1
replace1 = range(0,46)+range(58,127)+[47] #Makes a list of all the 
#ASCII characters  values that you don't want it to show,
#http://www.asciitable.com/, this includes all the letters,
#and excludes all numbers and '.'

text = '<html><body><p>127.0.0.1</p></body></html>' #Test data.
tmp = '' 

for i in range(len(text)-1): #this goes through each character in the text
...     if not ord(text[i]) in replace1: #checks if that character's 
#ASCII value is in not the list of 'Blacklisted' ASCII values, 
#then appends it to the tmp variable 
...             tmp += text[i]

print tmp
127.0.0.1
p99will
  • 250
  • 2
  • 3
  • 14