python - 我在我的 python 程序中遇到连接错误
问题描述
你试图运行我的程序,但每次我在运行过程中遇到错误时,我的程序都会这样做:1.从我的网站获取 xml 2.运行所有 url 3.从我的网页获取数据( sku、名称、标题、价格等) 4. 通过与相同 sku 比较价格,从另一个网站获得最低价格
问题是我的 xml 中有超过 7,000 个 url,所以我的程序每次都会出错网络怎么办?我该如何解决?
def parse_sitemap (url):
resp = requests.get(XXXX)
for u in urls:
loc = u.find ('loc').string
# not a sitemap requirement skip if not present
out.append ([loc])
return out
def get_sku (u):
html = requests.get(u)
bsObj = BeautifulSoup(html.content,'xml')
sku = bsObj.find('span',attrs={'itemprop':'sku'}).get_text()
return sku
def get_price ( u):
try:
html = requests.get(u)
bsObj = BeautifulSoup(html.content,'xml')
price = bsObj.find('span',attrs={'itemprop':'price'}).get_text()
price = str(price).replace(' ₪','')
return price
except:
return 'no price'
def get_zapPrice (makat):
try:
check ='https://www.zap.co.il/search.aspx?keyword='+makat
r = requests.get(check)
html = requests.get(r.url)
bsObj = BeautifulSoup(html.content,'html.parser')
zapPrice = bsObj.select_one('div.StoresLines div.PriceNum').text.strip().replace(' ₪','')
return zapPrice
except:
return 'no zap product'
def get_zapStoreName (makat):
try:
check ='https://www.zap.co.il/search.aspx?keyword='+makat
r = requests.get(check)
html = requests.get(r.url)
bsObj = BeautifulSoup(html.content,'html.parser')
storeName = bsObj.select_one('div.StoresLines
div.BuyButtonsTxt').text.strip().replace('ב-','')
return storeName
except:
return 'no zap product'
for u in urls:
ws1 [ 'A1' ] = u
makat = get_sku(u)
ws1 [ 'F1' ] = makat
zapPrice = get_zapPrice(makat)
ws1['I1'] = zapPrice
storeName = get_zapStoreName(makat)
ws1['J1'] = storeName
ws1.insert_rows(1)
ws1.append ([])
print("writing product no." + str(i))
ws1['F1'] = 'makat'
ws1['I1'] = 'zap price'
ws1['J1'] = 'zap store'
wb.save ("sample.xlsx")
wb.close ()
print ('end')
我没有写我所有的代码-基本在这里每个定义都以requests.get开头,得到我想要的然后返回它,我把它写到excel文件中
我在 1,000 个 url 检查后遇到的问题......有什么问题?
解决方案
推荐阅读
- fusionauth - Fusionauth 本身是身份提供者吗?
- c# - 什么模式可以用来创建一个类的对象,但以不同的方式填充它的属性?
- rest - 如果我在邮递员上传递了正确的 channelID keyID 和 secret,则在苹果新闻上获取 "code": "WRONG_SIGNATURE" 创建 api enen
- excel - 使用 VBA 从文本文件中提取数据时出错
- statusbar - 在 iOS 13 上,我的应用视图显示灰色视图
- c# - DataGridView 仅编辑列中的 1 行
- c++ - QMediaPlayer 命令中的 GST (gstreamer) 命令
- python - 如何在 jython 中导入模块?
- web-scraping - 有没有办法通过非 csv/tsv 格式的 IMPORTDATA 提取来拆分列
- c# - 如何在 Xamarin.Android 中显示 ProgressBar?