- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
只是想知道是否有人可以帮助我,因为我被击败了 - 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/
如果您想使用 String.Concat() 连接 5 个或更多字符串,则它会使用 Concat(String[])。 为什么不一直使用 Concat(String[]) 而不再需要 Concat(S
今天在使用 String 时,我遇到了一种我以前不知道的行为。我无法理解内部发生的事情。 public String returnVal(){ return "5";
似乎在我所看到的任何地方,都有一些过时的版本,这些版本不再起作用。 我的问题似乎很简单。我有一个Java类,它映射到derby数据库。我正在使用注释,并且已经成功地在数据库中创建了所有其他表,但是在这
一、string::size_type() 在C++标准库类型 string ,在调用size函数求解string 对象时,返回值为size_type类型,一种类似于unsigned类型的int 数据
我正在尝试将数据保存到我的 plist 文件中,其中包含字符串数组的定义。我的plist - enter image description here 我将数据写入 plist 的代码是 -- let
我有一个带有键/值对的 JavaScript 对象,其中值是字符串数组: var errors = { "Message": ["Error #1", "Error #2"], "Em
例如,为了使用相同的函数迭代 List 和 List> ,我可以编写如下内容: import java.util.*; public class Test{ public static voi
第一个Dictionary就像 Dictionary ParentDict = new Dictionary(); ParentDict.Add("A_1", "1")
这是我的 jsp 文件: 我遇到了错误 The method replace(String, String, String) in the type Functions is not appl
我需要一些帮助。我有一个方法应该输出一个包含列表内容的 txt 文件(每行中的每个项目)。列表项是字符串数组。问题是,当我调用 string.Join 时,它返回文字字符串 "System.Strin
一位同事告诉我,使用以下方法: string url = "SomeURL"; string ext = "SomeExt"; string sub = "SomeSub"; string s
给定类: public class CategoryValuePair { String category; String value; } 还有一个方法: public
我正在尝试合并 Stream>>对象与所有 Streams 中的键一起映射到单个映射中. 例如, final Map someObject; final List>> list = someObjec
在这里使用 IDictionary 的值(value)是什么? 最佳答案 使用接口(interface)的值(value)始终相同:切换到另一个后端实现时,您不必更改客户端代码。 请考虑稍后分析您的代
我可以知道这两个字典声明之间的区别吗? var places = [String: String]() var places = [Dictionary()] 为什么当我尝试以这种方式附加声明时,只有
在 .NET 4.0 及更高版本中存在 string.IsNullOrWhiteSpace(string) 时,在检查字符串时使用 string.IsNullOrEmpty(string) 是否被视为
这个名字背后的原因是什么? SS64在 PowerShell 中解释此处的字符串如下: A here string is a single-quoted or double-quoted string
我打算离开 this 文章,尝试编写一个接受字符串和 &str 的函数,但我遇到了问题。我有以下功能: pub fn new(t_num: S) -> BigNum where S: Into {
我有一个结构为 [String: [String: String]] 的多维数组。我可以使用 for 循环到达 [String: String] 位,但我不知道如何访问主键(这个位 [String:
我正在尝试使用 sarama(管理员模式)创建主题。没有 ConfigEntries 工作正常。但我需要定义一些配置。 我设置了主题配置(这里发生了错误): tConfigs := map[s
我是一名优秀的程序员,十分优秀!