答案 0 :(得分:2)
您的Title
和Link
实际上并不包含单个元素,但两者都包含所有标题和链接的列表(这些XPath表达式匹配多个元件)。
因此,为了获得title, link
对的列表,您需要zip()
他们在一起:
pairs = zip(titles, links)
完成后,您可以使用for
循环遍历这些对,并打印左对齐的项目,以便获得列:
print('{:<70}{}'.format(title, link))
(有关如何打印左对齐项目的详细信息,请参阅this answer。)
一切都在一起:
import requests
from lxml import html
url = "http://www.wiseowl.co.uk/videos/"
def startpoint(links):
response = requests.get(links)
tree = html.fromstring(response.text)
titles = tree.xpath("//p[@class='woVideoListDefaultSeriesTitle']/a/text()")
links = tree.xpath("//p[@class='woVideoListDefaultSeriesTitle']/a/@href")
pairs = zip(titles, links)
for title, link in pairs:
# Replace '70' with whatever you expect the maximum title length to be
print('{:<70}{}'.format(title, link))
startpoint(url)
答案 1 :(得分:1)
尝试按顺序迭代这两个列表,如下所示:
import requests
from lxml import html
url="http://www.wiseowl.co.uk/videos/"
def Startpoint(links):
response = requests.get(links)
tree = html.fromstring(response.text)
Title= tree.xpath("//p[@class='woVideoListDefaultSeriesTitle']/a/text()")
Link=tree.xpath("//p[@class='woVideoListDefaultSeriesTitle']/a/@href")
for i,j in zip(Title, Link):
print('{:<70}{}'.format(i,j))
Startpoint(url)
答案 2 :(得分:1)
您可以遍历每个链接并打印标题和网址。
import requests
from lxml import html
url="http://www.wiseowl.co.uk/videos/"
def Startpoint(links):
response = requests.get(links)
tree = html.fromstring(response.text)
links = tree.xpath("//p[@class='woVideoListDefaultSeriesTitle']/a")
for link in links:
print('{title:<70}{url}'.format(title=link.text, url=link.attrib.['href']))
Startpoint(url)