- 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/
我有一个关于 JavaScript 语法的问题。实际上,我在自学 MEAN 堆栈教程时想出了编码(https://thinkster.io/mean-stack-tutorial#adding-aut
在我的书中它使用了这样的东西: for($ARGV[0]) { Expression && do { print "..."; last; }; ... } for 循环不完整吗?另外,do 的意义何
我已经编写了读取开关状态的代码,如果按 3 次 # 则退出。 void allkeypadTest(void) { static uint8_t modeKeyCount=0; do
因此,对于上周我必须做的作业,我必须使用 4 个 do-while 循环和 if 语句在 Java 中制作一个猜谜游戏。我无法成功完成它,类(class)已经继续,没有为我提供任何帮助。如果有人可以查
int i=1,j=0,n=10,k; do{ j+=i; i<<1; printf("%d\n",i); // printf("%d\n",12<<1); }while
此代码用于基本杂货计算器的按钮。当我按下按钮时,一个输入对话框会显示您输入商品价格的位置。我遇到的问题是我无法弄清楚如何获得 do ... while 循环以使输入对话框在输入后弹出。 我希望它始终恢
当我在循环中修改字符串或另一个变量时,它的条件是否每次都重新计算?或者在循环开始前一次 std::string a("aa"); do { a = "aaaa"; } while(a.size<10)
我刚刚写了这个,但我找不到问题。我使用代码块并编写了这个问题 error: expected 'while' before '{' token === Build finished: 1 errors
do { printf("Enter number (0-6): ", ""); scanf("%d", &Num); }while(Num >= 0 && Num 表示“超过”,<表
我有一个包含 10 个项目的 vector (为简单起见,所有项目都属于同一类,称其为“a”)。我想要做的是检查“A”不是 a) 隐藏墙壁或 b) 隐藏另一个“A”。我有一个碰撞函数可以做到这一点。
嗨,这是我的第二个问题。我有下表 |-----|-------|------|------| |._id.|..INFO.|.DONE.|.LAST.| |..1..|...A...|...N..|.
这个问题在这里已经有了答案: 关闭 12 年前。 Possible Duplicates: Why are there sometimes meaningless do/while and if/e
来自 wikibook在 F# 上有一小部分它说: What does let! do?# let! runs an async object on its own thread, then it i
我在 Real World Haskell 书中遇到了以下函数: namesMatching pat | not (isPattern pat) = do exists do
我有一个类似于下面的用例,我创建了多个图并使用 gridExtra 将它们排列到一些页面布局中,最后使用 ggsave 将其保存为 PDF : p1 % mutate(label2
当我使用具有 for 循环的嵌套 let 语句时,如果没有 (do (html5 ..)),我将无法运行内部 [:tr]。 (defpartial column-settings-layout [&
执行 vagrant up 时出现此错误: anr@anr-Lenovo-G505s ~ $ vagrant up Bringing machine 'default' up with 'virtua
# ################################################# # Subroutine to add data to the table Blas
我想创建一个检查特定日期格式的读取主机。此外,目标是检查用户输入是否正确,如果不正确,则提示应再次弹出。 当我刚接触编程时,发现了这段代码,这似乎很合适。我仍然在努力“直到” do {
我关注这个tutorial在谷歌云机器学习引擎上进行培训。我一步一步地跟着它,但是在将 ml 作业提交到云时我遇到了错误。我运行了这个命令。 sam@sam-VirtualBox:~/models/r
我是一名优秀的程序员,十分优秀!