首页 > 解决方案 > 我在我的 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 检查后遇到的问题......有什么问题?

标签: pythonpython-3.xnetworkingerror-handling

解决方案


推荐阅读