Best way to clean up list items?

DFS nospam at dfs.com
Mon May 2 14:09:39 EDT 2016


On 5/2/2016 1:25 PM, Stephen Hansen wrote:
> On Mon, May 2, 2016, at 09:33 AM, DFS wrote:
>> Have: list1 = ['\r\n   Item 1  ','  Item 2  ','\r\n  ']
>
> I'm curious how you got to this point, it seems like you can solve the
> problem in how this is generated.

--------------------------------------------------------------------
from lxml import html
import requests

webpage = 
"http://www.usdirectory.com/ypr.aspx?fromform=qsearch&qs=TN&wqhqn=2&qc=Nashville&rg=30&qhqn=restaurant&sb=zipdisc&ap=2"

page  = requests.get(webpage)
tree  = html.fromstring(page.content)
addr1 = tree.xpath('//span[@class="text3"]/text()')
print 'Addresses: ', addr1
--------------------------------------------------------------------

I'd prefer to get clean data in the first place, but I don't know a 
better way to extract it from the HTML.






More information about the Python-list mailing list