【问题标题】:How do I list the print values from my Python script under each other and without the ','? [duplicate]如何列出我的 Python 脚本中的打印值,并且不使用“,”? [复制]
【发布时间】:2017-07-14 14:02:21
【问题描述】:

这是我的 Python 代码:

from lxml import html
import requests

page = requests.get('https://www.amazon.com/s/ref=nb_sb_noss/133-6356864-
3426147?url=search-alias%3Daps&field-keywords=usb')

tree = html.fromstring(page.content)
title = tree.xpath('//h2[@data-attribute]/text()')

print(title)

当我打印标题时,这是我得到的结果:

['SanDisk Cruzer 64GB USB 2.0 Flash Drive (SDCZ36-064G-B35)', 'SanDisk Cruzer CZ36 64GB USB 2.0 Flash Drive Frustration-Free Packaging- SDCZ36-064G-AFFP', 'SanDisk Cruzer Blade 32GB USB 2.0 Flash Drive Frustration-Free Packaging- SDCZ50-032G-AFFP', 'Samsung 32GB BAR (METAL) USB 3.0 Flash Drive (MUF-32BA/AM)', 'SanDisk Cruzer 128GB USB 2.0 Flash Drive (SDCZ36-128G-B35)', 'SanDisk Cruzer 8GB USB 2.0 Flash Drive (SDCZ36-008G-B35)', 'Sabrent 4-Port USB 2.0 Hub with Individual Power Switches and LEDs (HB-UMLS)', 'Kingston Digital DataTraveler SE9 32GB USB 2.0 Flash Drive Silver (DTSE9H/32GBZET)', 'SanDisk 16GB 2.0 Flash Cruzer Glide USB Drive (SDCZ60-016G-B35)', 'Samsung 64GB BAR (METAL) USB 3.0 Flash Drive (MUF-64BA/AM)', 'SanDisk Cruzer 16GB USB 2.0 Flash Drive (SDCZ36-016G-B35)', 'Micro USB OTG to USB 2.0 Adapter; SD/Micro SD Card Reader With Standard USB Male & Micro USB Male Connector For Smartphones/Tablets With OTG Function', 'SanDisk Cruzer Fit CZ33 16GB USB 2.0 Low-Profile Flash Drive- SDCZ33-016G-B35', 'SanDisk Cruzer Blade 32GB USB 2.0 Flash Drive- SDCZ50-032G-B35', 'Sabrent 4-Port USB 3.0 Hub with Individual Power Switches and LEDs (HB-UM43)', 'Samsung 64GB USB 3.0 Flash Drive Fit (MUF-64BB/AM)']

是否有某种方法可以创建一个列表,其中产品的每个标题都列在彼此下方并且没有“,”?

非常感谢您的帮助!

【问题讨论】:

  • for x in title: print(x)

标签: python list python-3.x web-scraping


【解决方案1】:

遍历列表中的项目并分别打印。

for t in title:
    print(t)

【讨论】:

    猜你喜欢
    • 2021-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多