gpt4 book ai didi

python - 将数据保存为新行,但在单个单元格中 lxml python

转载 作者:行者123 更新时间:2023-12-01 09:06:43 24 4
gpt4 key购买 nike

我想要这样的数据...

“基本款 Jersey

按照 jar 头上所说的做

主要成分:100% 棉。”

在一个单元格中,但我得到的数据是这样的......

“基本款平纹针织 Fabric 正如 jar 头上所说的那样主要:100% 棉。”

这是 HTML

<div class="about-me">
<h4>ABOUT ME</h4>
<span><div>Basic jersey</div><div>Does what it says on the tin</div><br>Main: 100% Cotton.</span>
</div>
这是我的代码
from selenium import webdriver
from lxml import html
import pandas as pd
import collections, os
from bs4 import BeautifulSoup

def Save_to_Csv(data):
filename = 'data.csv'
df = pd.DataFrame(data)
df.set_index('Title', drop=True, inplace=True)
if os.path.isfile(filename):
with open(filename,'a') as f:
df.to_csv(f, mode='a', sep=",", header=False, encoding='utf-8')
else:
df.to_csv(filename, sep=",", encoding='utf-8')

with open('urls.txt', 'r') as f:
links = [link.strip() for link in f.readlines()]
driver = webdriver.Chrome()
for urls in links:
global image
driver.get(urls)
source = driver.page_source
tree = html.fromstring(source)
data = BeautifulSoup(source, 'html.parser')
imgtag = data.find_all('li', attrs={'class':'image-thumbnail'})
image = []
for imgsrc in imgtag:
image.append(imgsrc.img['src'].replace('?$S$&wid=40&fit=constrain', '?$XXL$&wid=513&fit=constrain'))
title = tree.xpath('string(.//div/h1)')
price = tree.xpath('string(.//span[@class="current-price"])')
sku = tree.xpath('string(.//div[@class="product-code"]/span)')
aboutme = tree.xpath(('string(.//div[@class="about-me"]/span)'))

foundings = collections.OrderedDict()
foundings['Title'] = [title]
foundings['Price'] = [price]
foundings['Product_Code'] = [sku]
foundings['Abouy_Me'] = [aboutme]
foundings['Image'] = [image]
Save_to_Csv(foundings)

print title, price, sku, aboutme, image
driver.close()

最佳答案

使用您提供的 HTML,您可以使用 stripped_strings 解决此问题生成器如下:

from bs4 import BeautifulSoup

html = """
<div class="about-me">
<h4>ABOUT ME</h4>
<span><div>Basic jersey</div><div>Does what it says on the tin</div><br>Main: 100% Cotton.</span>
</div>"""

soup = BeautifulSoup(html, "html.parser")

print('\n'.join(soup.span.stripped_strings))

这会将每个组件放入剥离列表中,然后用换行符将它们连接在一起:

Basic jersey
Does what it says on the tin
Main: 100% Cotton.

关于python - 将数据保存为新行,但在单个单元格中 lxml python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51986817/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com