当我运行爬虫时,它会以列表形式获取结果。但是,我希望将其以常规字符串的形式显示在两列中。感谢您的任何建议。
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")
print(Title,Link)
Startpoint(url)
最佳答案
您的Title
和Link
实际上不包含单个元素,但两者分别包含所有标题和链接的列表(这些XPath 表达式匹配多个元素)。
因此,为了获取标题、链接
对的列表,您需要 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)
关于python - 无法将抓取的数据从列表转换为常规字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43924526/