gpt4 book ai didi

python - 使用 Python 将漂亮的汤抓取到 MySQL。卡在 if string contains do else 做其他事情

转载 作者:行者123 更新时间:2023-11-29 15:33:44 25 4
gpt4 key购买 nike

只是想知道是否有人可以帮助我,因为我被击败了 - python 新手,这让我在过去 4 小时内不断尝试和犯错,我已经到了在我的修正案中迷失的地步。

我卡住的部分是:

枪管长度英寸枪管长度分数

下面的片段:


barrellengths = soup.find(barrellength_span)
gun_barrellengths = barrellengths.text if barrellengths else ''
gun_barrellengths_inches = ''
gun_barrellengthfraction = ''

#if " in" present split the string to print the inches
def barrel_length_inches_text(gun_barrellengths_inches):
if " in" in gun_barrellengths:
gun_barrellengths_inches = gun_barrellengths.split()[0]
else:
gun_barrellengths_inches = '0'

#if " present check to see if there can be a split for the fraction else remove the " and continue
def barrel_length_inches_symbol(gun_barrellengths_inches):
if '"' in gun_barrellengths:
try:
gun_barrellengths.split()
gun_barrellengths_inches = gun_barrellengths.split()[0]
except:
IndexError
gun_barrellengths_inches = gunbarrellengths.replace('"','')

#which method to use
def barrel_length_inches(gun_barrellengths_inches):
if(len(gun_barrellengths) == 0):
gun_barrellength_inches = ''
elif " in" in gun_barrellengths:
barrel_length_inches_text(gun_barrellengths_inches)
elif '"' in gun_barrellengths:
barrel_length_inches_symbol(gun_barrellengths_inches)


#if there is a decimal point in barrellengths
def barrel_length_fraction_symbol(gun_barrellength_fraction):
if '.' in gun_barrellengths:
try:
gun_barrellengths.split()
gun_barrellengthfraction = gun_barrellengths.split()[1]
gun_barrellengthfraction = gun_barrelfraction.replace('"','')
gun_barrellengthfraction = 0+gun_barrelfraction
except:
IndexError
gun_barrellengthfraction = '0'

#if there is text in barrel length fraction
def gun_barrel_length_fraction_text(gun_barrellength_fraction):
if ' in' in gun_barrellengths:
try:
gun_barrellengthfraction = gun_barrellengths.split()[1]
if "1/2" in gun_barrellengthfraction:
gun_barrellengthfraction = gun_barrellengthfraction.replace("1/2", "0.5")
elif "1/4" in gun_barrellengthfraction:
gun_barrellengthfraction = gun_barrellengthfraction.replace("1/4", "0.25")
elif "3/4" in gun_barrellengthfraction:
gun_barrellengthfraction = gun_barrellengthfraction.replace("3/4", "0.75")
elif "in" in gun_barrellengthfraction:
gun_barrellengthfraction = '0'
except:
IndexError
gun_barrellengthfraction = '0'

#decide which function works for the fraction
def barrel_length_fraction(gun_barrellengthfraction):
if(len(gun_barrellengths) == 0):
gun_barrellengthfraction = ''
elif "." in gun_barrellengths:
gun_barrel_length_fraction_symbol(gun_barrellengthfraction)
elif "in" in gun_barrellengths:
gun_barrel_length_fraction_text(gun_barrellengthfraction)

当我推出这个时,我的数组输出得到 None, None - 但那里肯定有数据。

我正在尝试检查数据,如果有“32”,请删除“,这样它就只是“32”,分数为 0如果数据显示为 32.5",则在“.”上拆分字符串将 32 放入gun_barrellengths_inches 变量并将 .5 放入gun_barrellengthfraction 变量

或者,如果数据显示为“32 in”,则删除“in”,并将gun_barrellengths_inches设为32,gun_barrellengthfraction为0。如果“32 1/2 英寸”在第一个“”上拆分并使枪管长度为 32,则将“1/2 英寸”替换为 0.5 并使枪管长度分数为 0.5'

URL 会随着经销商的不同而改变 - 我有一个适用于该特定经销商的代码,并且很高兴(在一些帮助下,我花了几周的时间改进了这个代码)。但是当我与另一个经销商测试此脚本时,它在这些上失败了(并且在库存上也是相同的 - 但将在其中实现这些更改)

完整代码如下:

from bs4 import BeautifulSoup
import requests
import shutil
import csv
import pandas
from pandas import DataFrame
import re
import os
import io
import urllib
import locale
import math
os.environ["PYTHONIOENCODING"] = "utf-8"
import mysql.connector
from mysql.connector import errorcode


cnx = mysql.connector.connect(user='user', password='password', host='127.0.0.1', port='3306', database='DatabaseName')

cursor = cnx.cursor()

page = 1
all_links = []
url="https://www.gunstar.co.uk/view-trader/global-rifle-snipersystems/58782?page={}"

with requests.Session() as session:
while True:
print(url.format(page))
res=session.get(url.format(page))
soup=BeautifulSoup(res.content,'html.parser')
gun_details = soup.select('div.details')
for link in gun_details:
all_links.append("https://www.gunstar.co.uk" + link.select_one('a')['href'])

if len(soup.select('a.al-pagination-item'))==0:
break
page += 1

print(len(all_links))

gunstar_id = 0

for a_link in all_links:

gunstar_id += 1

def category_span(category):
return category.name=='span' and 'Category' in category.parent.contents[0]

def subCategory_span(subCategory):
return subCategory.name=='span' and 'Subcategory' in subCategory.parent.contents[0]

def make_span(make):
return make.name=='span' and 'Make' in make.parent.contents[0]

def model_span(model):
return model.name=='span' and 'Model' in model.parent.contents[0]

def mechanism_span(mechanism):
return mechanism.name=='span' and 'Mechanism' in mechanism.parent.contents[0]

def calibre_span(calibre):
return calibre.name=='span' and 'Calibre' in calibre.parent.contents[0]

def licence_span(licence):
return licence.name=='span' and 'Certificate' in licence.parent.contents[0]

def orientation_span(orientation):
return orientation.name=='span' and 'Orientation' in orientation.parent.contents[0]

def barrellength_span(barrellength):
return barrellength.name=='span' and 'Barrel length' in barrellength.parent.contents[0]

def stocklength_span(stocklength):
return stocklength.name=='span' and 'Stock length' in stocklength.parent.contents[0]

def gunlength_span(gunlength):
return gunlength.name=='span' and 'Gun length' in gunlength.parent.contents[0]

def weight_span(weight):
return weight.name=='span' and 'Weight' in weight.parent.contents[0]

def chamber_span(chamber):
return chamber.name=='span' and 'Chamber length' in chamber.parent.contents[0]

def chokes_span(chokes):
return chokes.name=='span' and 'Chokes' in chokes.parent.contents[0]

def ejection_span(ejection):
return ejection.name=='span' and 'Ejection' in ejection.parent.contents[0]

def trigger_span(trigger):
return trigger.name=='span' and 'Trigger' in trigger.parent.contents[0]

def condition_span(condition):
return condition.name=='span' and 'Condition' in condition.parent.contents[0]

def price_span(price):
return price.name=='span' and 'Price' in price.parent.contents[0]

res = requests.get(a_link)
soup = BeautifulSoup(res.content, 'html.parser')

gun_details = soup.findAll('div', {"class":"mb al-spec flex"})

categorys = soup.find(category_span)
gun_categorys = categorys.text if categorys else ''

subCategorys = soup.find(subCategory_span)
gun_subCategorys = subCategorys.text if subCategorys else ''

makes = soup.find(make_span)
gun_makes = makes.text if makes else ''

models = soup.find(model_span)
gun_models = models.text if models else ''

mechanisms = soup.find(mechanism_span)
gun_mechanisms = mechanisms.text if mechanisms else ''

calibres = soup.find(calibre_span)
gun_calibres = calibres.text if calibres else ''
if "12 Bore/gauge" in gun_calibres:
gun_calibres = gun_calibres.replace("12 Bore/gauge", "12 Gauge")
else:
gun_calibres


#licences = soup.find(licence_span)
#gun_licences = licences.text if licences else ''
if "Rifles" in gun_categorys:
gun_licences = "FAC"
elif "Shotguns" in gun_categorys:
gun_licences = "FAC/SGC"
else:
gun_licences = ''

orientations = soup.find(orientation_span)
gun_orientations = orientations.text if orientations else ''

barrellengths = soup.find(barrellength_span)
gun_barrellengths = barrellengths.text if barrellengths else ''
gun_barrellengths_inches = ''
gun_barrellengthfraction = ''

#if " in" present split the string to print the inches
def barrel_length_inches_text(gun_barrellengths_inches):
if " in" in gun_barrellengths:
gun_barrellengths_inches = gun_barrellengths.split()[0]
else:
gun_barrellengths_inches = '0'

#if " present check to see if there can be a split for the fraction else remove the " and continue
def barrel_length_inches_symbol(gun_barrellengths_inches):
if '"' in gun_barrellengths:
try:
gun_barrellengths.split()
gun_barrellengths_inches = gun_barrellengths.split()[0]
except:
IndexError
gun_barrellengths_inches = gunbarrellengths.replace('"','')

#which method to use
def barrel_length_inches(gun_barrellengths_inches):
if(len(gun_barrellengths) == 0):
gun_barrellength_inches = ''
elif " in" in gun_barrellengths:
barrel_length_inches_text(gun_barrellengths_inches)
elif '"' in gun_barrellengths:
barrel_length_inches_symbol(gun_barrellengths_inches)


#if there is a decimal point in barrellengths
def barrel_length_fraction_symbol(gun_barrellength_fraction):
if '.' in gun_barrellengths:
try:
gun_barrellengths.split()
gun_barrellengthfraction = gun_barrellengths.split()[1]
gun_barrellengthfraction = gun_barrelfraction.replace('"','')
gun_barrellengthfraction = 0+gun_barrelfraction
except:
IndexError
gun_barrellengthfraction = '0'

#if there is text in barrel length fraction
def gun_barrel_length_fraction_text(gun_barrellength_fraction):
if ' in' in gun_barrellengths:
try:
gun_barrellengthfraction = gun_barrellengths.split()[1]
if "1/2" in gun_barrellengthfraction:
gun_barrellengthfraction = gun_barrellengthfraction.replace("1/2", "0.5")
elif "1/4" in gun_barrellengthfraction:
gun_barrellengthfraction = gun_barrellengthfraction.replace("1/4", "0.25")
elif "3/4" in gun_barrellengthfraction:
gun_barrellengthfraction = gun_barrellengthfraction.replace("3/4", "0.75")
elif "in" in gun_barrellengthfraction:
gun_barrellengthfraction = '0'
except:
IndexError
gun_barrellengthfraction = '0'

#decide which function works for the fraction
def barrel_length_fraction(gun_barrellengthfraction):
if(len(gun_barrellengths) == 0):
gun_barrellengthfraction = ''
elif "." in gun_barrellengths:
gun_barrel_length_fraction_symbol(gun_barrellengthfraction)
elif "in" in gun_barrellengths:
gun_barrel_length_fraction_text(gun_barrellengthfraction)



stocklengths = soup.find(stocklength_span)
gun_stocklengths = stocklengths.text if stocklengths else ''
if " in" in gun_stocklengths:
gun_stocklength_inches = gun_stocklengths.split()[0]
else:
gun_stocklength_inches = ''

if(len(gun_stocklengths) == 0):
gun_stocklength_fraction = ''
else:
gun_stocklength_fraction = gun_stocklengths.split()[1]
if "1/2" in gun_stocklength_fraction:
gun_stocklength_fraction = gun_stocklength_fraction.replace("1/2", "0.5")
elif "1/4" in gun_stocklength_fraction:
gun_stocklength_fraction = gun_stocklength_fraction.replace("1/4", "0.25")
elif "3/4" in gun_stocklength_fraction:
gun_stocklength_fraction = gun_stocklength_fraction.replace("3/4", "0.75")
elif "in" in gun_stocklength_fraction:
gun_stocklength_fraction = ''


gunlengths = soup.find(gunlength_span)
gun_gunlengths = gunlengths.text if gunlengths else ''



weights = soup.find(weight_span)
gun_weights = weights.text if weights else ''
if " kilo" in gun_weights:
gun_weight_lb = gun_weights.split()[0]
gun_weight_lb = float(gun_weight_lb)
gun_weight_lb = gun_weight_lb * 2.2046226218
gun_weight_lb = float(gun_weight_lb)
gun_weight_lb_oz = str(gun_weight_lb)
gun_weight_lb_round = math.ceil(gun_weight_lb) #converting to whole int
gun_weight_lb_round = str(gun_weight_lb_round)
else:
gun_weight_lb_round = ''

if(len(gun_weight_lb_round) == 0):
gun_weight_oz = ''
else:
gun_weight_oz = "0."+gun_weight_lb_oz.split('.')[1] #cant split cause not float
gun_weight_oz = float(gun_weight_oz)
gun_weight_oz = gun_weight_oz * 16
gun_weight_oz = math.ceil(gun_weight_oz)


chambers = soup.find(chamber_span)
gun_chambers = chambers.text if chambers else ''
if " in" in gun_chambers:
gun_chambers = gun_chambers.split()[0]
else:
gun_chambers = ''

chokess = soup.find(chokes_span)
gun_chokess = chokess.text if chokess else ''

ejections = soup.find(ejection_span)
gun_ejections = ejections.text if ejections else ''

triggers = soup.find(trigger_span)
gun_triggers = triggers.text if triggers else ''

conditions = soup.find(condition_span)
gun_conditions = conditions.text if conditions else ''

prices = soup.find(price_span)
gun_prices = prices.text if prices else ''
if "£ " in gun_prices:
gun_prices = gun_prices.split()[1]
if "," in gun_prices:
gun_prices = gun_prices.replace(',', '')
if " each" in gun_prices:
gun_prices = gun_prices.replace(' each', '')
else:
gun_prices = ''

gun_description = soup.find('div', {'class':'al-addet-desc-text t-bd-14 mb-4'})
if (gun_description is not None):
gun_description_text = gun_description.text
else:
gun_description_text = ''
if "...Read full description" in gun_description_text:
gun_description_text = gun_description_text.replace("...Read full description", "")
else:
gun_description_text

imgs = soup.findAll("img", {"class":"al-mediabar-item-img js-trigger-slideshow"}) #Keep - Photos save
# gundir = soup.find("title").text #keep - folder creation for each advert using title
# gun_folders = os.makedirs(gundir)


# for img in imgs:
# clean = re.compile('src=".*?"')
# strings = clean.findall(str(img))
# for string in strings:
# imgUrl = string.split('"')[1]
# filename = imgUrl.split('/')[-1]
# resp = requests.get(imgUrl, stream=True)
# local_file = open('{}/{}'.format(gundir ,filename), 'wb')
# resp.raw.decode_content = True
# shutil.copyfileobj(resp.raw, local_file)
# del resp

array = [gunstar_id, gun_categorys, gun_subCategorys, gun_makes, gun_models, gun_mechanisms, gun_calibres, gun_licences, gun_orientations, barrel_length_inches(gun_barrellengths_inches), barrel_length_fraction(gun_barrellengthfraction), gun_stocklength_inches, gun_stocklength_fraction, gun_gunlengths, gun_weight_lb_round, gun_weight_oz, gun_chambers, gun_chokess, gun_ejections, gun_triggers, gun_conditions, gun_prices, gun_description_text]

print(array)

cursor.execute("INSERT INTO tbl_gunstar_test VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)", array)

cursor.close()
cnx.close()

最佳答案

    barrellengths = soup.find(barrellength_span)
gun_barrellengths = barrellengths.text if barrellengths else ''
gun_barrellengths_inches = ''
gun_barrellengthfraction = ''

#if " in" present split the string to print the inches
def barrel_length_inches(gun_barrellengths_inches):
if " in" in gun_barrellengths:
gun_barrellengths_inches = gun_barrellengths.split()[0]
return gun_barrellengths_inches
elif '.' not in gun_barrellengths:
gun_barrellengths_inches = gun_barrellengths.split()[0]
gun_barrellengths_inches = gun_barrellengths_inches.replace('"','')
return gun_barrellengths_inches
elif '.' in gun_barrellengths:
gun_barrellengths_inches = gun_barrellengths.split('.')[0]
return gun_barrellengths_inches
else:
gun_barrellengths_inches = ''
return gun_barrellengths_inches

print(barrel_length_inches(gun_barrellengths_inches))

#if there is a decimal point in barrellengths
def barrel_length_fraction(gun_barrellengthfraction):
if '.' in gun_barrellengths:
gun_barrellengthfraction = gun_barrellengths.split('.')[1]
gun_barrellengthfraction = gun_barrellengthfraction.replace('"','')
gun_barrellengthfraction = '0.'+gun_barrellengthfraction
return gun_barrellengthfraction
elif ' in' in gun_barrellengths:
gun_barrellengthfraction = gun_barrellengths.split()[1]
if "1/2" in gun_barrellengthfraction:
gun_barrellengthfraction = gun_barrellengthfraction.replace("1/2", "0.5")
return gun_barrellengthfraction
elif "1/4" in gun_barrellengthfraction:
gun_barrellengthfraction = gun_barrellengthfraction.replace("1/4", "0.25")
return gun_barrellengthfraction
elif "3/4" in gun_barrellengthfraction:
gun_barrellengthfraction = gun_barrellengthfraction.replace("3/4", "0.75")
return gun_barrellengthfraction
elif "in" in gun_barrellengthfraction:
gun_barrellengthfraction = ''
return gun_barrellengthfraction
else:
gun_barrellengthfraction = ''
return gun_barrellengthfraction

print(barrel_length_fraction(gun_barrellengthfraction))

关于python - 使用 Python 将漂亮的汤抓取到 MySQL。卡在 if string contains do else 做其他事情,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58465625/

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