作者girl5566 (5566520)
看板Python
标题[问题] lxml遇到<br /> 该如何处理?
时间Mon Mar 14 23:06:54 2016
大家好 最近想试着撰写网页爬虫
想抓取网页的这部分资讯
http://imgur.com/rNdE4hh
尝试的结果为
# -*- coding: utf-8 -*-
from urllib2 import urlopen
import xml.etree.ElementTree as ET
from lxml import etree
import mechanize
import sys
url = "
http://www.tham.com.tw/recipe6.php"
path = "//*[@id=\"left-inner\"]/div[2]/div[3]"
html = urlopen(url).read()
tree = etree.HTML(html)
startindex = 4
data = tree.xpath(path)
print data[0].text
Output:
>>> ================================ RESTART ================================
>>>
材料 2人份
>>>
看网页的原始码猜测是因为<br />阻挡了判断的缘故
请问这个有解吗??
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 123.195.222.114
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1457968017.A.79E.html
1F:推 ckc1ark: //*[@id=\"left-inner\"]/div[2]/div[3]//text() 试试 03/15 00:37
2F:→ girl5566: 感谢 已解决 03/15 19:43
请在请教一下 xpath这部分要怎麽debug? 有什麽秘诀吗?
下面output也怪怪的
# -*- coding: utf-8 -*-
from urllib2 import urlopen
import xml.etree.ElementTree as ET
from lxml import etree
import mechanize
import sys
url = "
https://icook.tw/recipes/133425"
html = urlopen(url).read()
tree = etree.HTML(html)
path = "//*[@id=\"recipes_show\"]/div[3]"
title = tree.xpath(path)
print title
Output:
>>>
[]
※ 编辑: girl5566 (123.195.222.114), 03/15/2016 20:24:59
3F:推 aweimeow: path = "//*[@itemprop=\"name\"]" 03/16 20:18
4F:→ aweimeow: print title[0].text 03/16 20:19
5F:→ aweimeow: 你的 XPATH 抓错了 03/16 20:19