如何从<a> element using lxml cssselctor?

时间:2018-02-27 17:57:54

标签: python-3.x beautifulsoup lxml lxml.html

def extract_page_data(html):
tree = lxml.html.fromstring(html)
item_sel = CSSSelector('.my-item')
text_sel = CSSSelector('.my-text-content')
time_sel = CSSSelector('.time')
author_sel = CSSSelector('.author-text')
a_tag = CSSSelector('.a')

    for item in item_sel(tree):
    yield {'href': a_tag(item)[0].text_content(),
           'my pagetext': text_sel(item)[0].text_content(),
           'time': time_sel(item)[0].text_content().strip(),
           'author': author_sel(item)[0].text_content()}

I want to extract href but I am not able to extract it using this code

1 个答案:

答案 0 :(得分:3)

尝试将@href提取为

'href': a_tag(item)[0].attrib['href']

'href': a_tag(item)[0].get('href')

作为选项,您也可以使用XPath

tree.xpath(".//a/@href")