python,多次运行def()

python,多次运行def(),python,input,Python,Input,我写这段代码是为了得到易趣的价格 它要求完整的易趣链接,然后写下价格 import bs4 , requests print('please enter full Ebay link ..') link = str(input()) def ebayprice(url): res = requests.get(link) res.raise_for_status() txt = bs4.BeautifulSoup(res.text , 'html.parser')

我写这段代码是为了得到易趣的价格 它要求完整的易趣链接,然后写下价格

import bs4 , requests
print('please enter full Ebay link  ..')
link = str(input())
def ebayprice(url):
    res = requests.get(link)
    res.raise_for_status()

    txt = bs4.BeautifulSoup(res.text , 'html.parser')
    csselement = txt.select('#mm-saleDscPrc')
    return csselement[0].text.strip()
price = ebayprice(link)

print('price is : '+ price)
我想改进它,我尽了最大的努力,但我做不到 我希望它采取多个链接,并运行他们一个接一个,它应该写的结果每次
链接是来自input()还是来自links='wwww1,www2,www3'

都无所谓。您可以用逗号分割链接,然后使用
for循环在
列表中迭代:

def ebayprice(url):
    ...

for single_link in link.split(','):
    price = ebayprice(single_link)
    print('price for {} is {}'.format(single_link, price))

如果你想,你可以问有多少链接,有人想刮,然后你可以使用for loop statment遍历每个url

import bs4 , requests
# ask how many links he will pass
print('How many links do you want wo scrape ?')
link_numb = int(input())

# get the links 
print('please enter full Ebay link  ..')
links = [input() for _ in range(link_numb)]

def ebayprice(link):
    res = requests.get(link)
    res.raise_for_status()

    txt = bs4.BeautifulSoup(res.text , 'html.parser')
    csselement = txt.select('#mm-saleDscPrc')
    return csselement[0].text.strip()

for link in links:
    price = ebayprice(link)
    print(price)
例如:

How many links do you want wo scrape ?
2
please enter full Ebay link  ..
http://example.com
http://example-just-test.com
# simple print the url
http://example.com
http://example-just-test.com

使用循环语句您应该将
res=requests.get(link)
替换为
res=requests.get(url)
。它当前使用全局链接,而不是作为参数提供的url