小编elr*_*man的帖子

如何使用lxml csssselctor从<a>元素中提取href?

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()}
Run Code Online (Sandbox Code Playgroud)

我想提取href但我无法使用此代码提取它

lxml beautifulsoup python-3.x lxml.html

1
推荐指数
1
解决办法
983
查看次数

标签 统计

beautifulsoup ×1

lxml ×1

lxml.html ×1

python-3.x ×1