无法操纵已解析的字符串

时间:2017-09-09 21:17:24

标签: python string python-3.x web-scraping

我创建了一个解析器,使用python从网页获取地址和邮政编码。我的解析器无条件运行。结果分为两行。但是,我在操纵弦乐方面非常弱。所以,如果有人帮助我,我会的 非常感谢。感谢。

这是脚本:

import requests ; from lxml import html

link = "http://www.greenthumbnyc.org/gardensearch.html#map-canvas"

def green_thumb(base_link):
    response = requests.get(base_link)
    tree = html.fromstring(response.text)
    title = tree.cssselect(".garden-info p")[0].text_content().replace("More information","")
    print(title)

green_thumb(link)

结果我有:

138th Street Community Garden (El Girasol)
624-638 East 138th Street

结果我想:

138th Street Community Garden (El Girasol) 624-638 East 138th Street

1 个答案:

答案 0 :(得分:1)

尝试添加.replace("\n","")

title = tree.cssselect(".garden-info p")[0].text_content().replace("More information","").replace("\n","")

希望这有帮助。