【发布时间】:2014-05-17 05:19:59
【问题描述】:
我有一个存储在变量 href 中的 URL 列表。当我通过下面的函数传递它时,唯一返回的 recipe_links 来自href 中的第一个 URL。我的代码有什么明显的错误吗?我不确定为什么它不会遍历我存储在href 中的所有 20 个 URL。我为href 中的第一个 URL 获得的返回结果按预期检索,但我无法获取到下一个 URL 的循环。
def first_page_links(link):
recipe_links = []
recipe_html = []
for x in link:
page_request = requests.get(x)
recipe_html.append(html.fromstring(page_request.text))
print recipe_html
for x in recipe_html:
recipe_links.append(x.xpath('//*[@id="content"]/ul/li/a/@href'))
return recipe_links
【问题讨论】:
标签: python html html-parsing lxml python-requests