简体   繁体   English

在Python中将两个文本合并为一个

[英]Combine two texts into one in Python

I am trying to scrape a website. 我正在尝试抓取一个网站。 I have got the information I need but I can't figure out how to output the prod title followed by a space and then the amount and then keep doing down until the end. 我已经获得了所需的信息,但是我无法弄清楚如何输出产品标题,然后输出空格,然后输出数量,然后继续进行下去,直到最后。

g_data = soup.find_all("h4", {"class": "prod-title"})
p_data = soup.find_all("span", {"class": "amount"})

for item in g_data:
    print (item.text)
for item in p_data:
    print (item.text)

print ("g_data" + "p_data")

Use zip() . 使用zip()

for g, p in zip(g_data, p_data):
    print(g.text, p.text)

You can use the zip() function. 您可以使用zip()函数。

Usage: 用法:

for a, b in zip(g_data, p_data):
    print(a.text, b.text)

Documentation for zip - https://docs.python.org/3/library/functions.html#zip 压缩文件-https: //docs.python.org/3/library/functions.html#zip

You can use izip function https://docs.python.org/2/library/itertools.html#itertools.izip which returns an iterator instead of the list 您可以使用izip函数https://docs.python.org/2/library/itertools.html#itertools.izip ,该函数返回迭代器而不是列表

izipped = izip(iter1, iter2) izipped = izip(iter1,iter2)

for x in izipped: print(str(x[0], str(x[1])) 对于x压缩格式:print(str(x [0],str(x [1]))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM