通过xml的Python抓取将打印空括号

2024-09-27 23:17:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图通过lxml从一个网站中提取一些字符,然后是树,然后是xpath。我尝试过使用googlechrome来获得正确的xpath,但是它会打印空括号。在

    #imports
    from lxml import html
    import requests

    #get magicseaweed Scripps report
    msScrippsPage = requests.get("""http://magicseaweed.com/Scripps-Pier-
    La-Jolla-Surf-Report/296/.html""")

    #make tree from site
    msScrippsTree = html.fromstring(msScrippsPage.content)

    #get wave size
    msScrippsWave = msScrippsTree.xpath("""/html/body/div[2]/div[5]/div/div[1]/div[2]/div[2]/div/div[2]/div[1]/div/div[1]/div/div/div/div/div[1]/div/div[2]/ul[1]/li[1]/text()""")

    print 'ms SCripps: ', msScrippsWave

输出到终端是'msScripps:[]'


Tags: fromimportdivget网站html字符requests
1条回答
网友
1楼 · 发布于 2024-09-27 23:17:31

你不应该在你的url中使用换行符。当您使用一行时,您的xpath工作。在

msScrippsPage = requests.get("""http://magicseaweed.com/Scripps-Pier-La-Jolla-Surf-Report/296/.html""")
print msScrippsPage.content
['    0.4-0.6', '   ']
########################################
url = """http://magicseaweed.com/Scripps-Pier-
La-Jolla-Surf-Report/296/.html"""
print url
'http://magicseaweed.com/Scripps-Pier-\n    La-Jolla-Surf-Report/296/.html'

编辑:添加完整示例

^{pr2}$

相关问题 更多 >

    热门问题