实际上,我正在做一个网页清理项目,但我遇到了问题
问题是,我想用base64格式对产品的图像进行编码,并将其保存到pandas数据框中,但当我执行代码时,没有出现错误,但发生了什么,不是所有的产品都存储在数据框中,我不知道为什么
否则,当我删除编码base64代码时,一切正常
我还尝试使用多个数据帧,我认为问题在于一个数据帧可以处理的大小
这里是代码请帮忙谢谢
def product():
full_name=''
npo_produit={}
produit_no=0
with open ('produitTest.txt' , 'r', encoding='utf-8') as file :
for row in file :
response = urlopen(row.strip().replace('é','%C3%A9').replace('è','%C3%A8').replace('ï','%C3%AF').replace('â','%C3%A2').replace('à','%C3%A0').replace('œ','%C5%93').replace('ç','%C3%A7').replace('ɧ','%C9%A7').replace('ô','%C3%B4'))
s =BeautifulSoup(response,'html.parser')
produit = s.find('h1',{'class':'productpage_title'})
marque = s.find('div',{'class':'marque'}).find('span')
poids = s.find('div',{'class':'product-information'}).find('dd',{'class':'value'})
reference = s.find('div',{'class':'product-information'}).find('div',{'class':'product-information'}).find('dd',{'class':'value'})
prix = s.find('div',{'class':'current-price'})
image = s.find('img',{'class':'js-qv-product-cover'})
src=image.get('src')
print(src)
if src!='':
img_name=random.randrange(1,1000000)
full_name=str(img_name)+'.jpg'
urllib.request.urlretrieve(src.replace('é','%C3%A9').replace('è','%C3%A8').replace('ï','%C3%AF').replace('â','%C3%A2').replace('à','%C3%A0').replace('œ','%C5%93').replace('ç','%C3%A7').replace('ɧ','%C9%A7').replace('ô','%C3%B4'),full_name)
with open( str(img_name)+'.jpg' ,'rb') as imagefile:
byteform=base64.b64encode(imagefile.read())
else :
src='https://clickandcollect.monoprix.tn/modules/tm_imageslider/views/img/2d363370a3317065692d02ca331976aad8e915ca_Banner-mpx1--produits-frais.png'
img_name=random.randrange(1,1000000)
full_name=str(img_name)+'.jpg'
urllib.request.urlretrieve(src,full_name)
with open( str(img_name)+'.jpg' ,'rb') as imagefile:
byteform=base64.b64encode(imagefile.read())
print(produit.text,marque.text,poids.text,reference.text.replace('Référence:',' '),prix.text.replace('\n',''),byteform)
produit_no+=1
npo_produit[produit_no]=[produit.text,marque.text,poids.text,reference.text.replace('Référence:',' '),prix.text.replace('\n',''),byteform]
if produit_no<=519:
df=pd.DataFrame.from_dict( npo_produit,orient='index' , columns = ['produit','marque','poids','reference','prix','image'])
elif produit_no>519 and produit_no<=1038 :
df1=pd.DataFrame.from_dict( npo_produit,orient='index' , columns = ['produit','marque','poids','reference','prix','image'])
elif produit_no>1038:
df2=pd.DataFrame.from_dict( npo_produit,orient='index' , columns = ['produit','marque','poids','reference','prix','image'])
time.sleep(10)
frames = [df, df1, df2]
result = pd.concat(frames)
return result
df_new=product()
目前没有回答
相关问题 更多 >
编程相关推荐