我希望url能够改变5次,并且每次{}
都用不同的名称完成。
我写的代码:
import bs4
import requests
from bs4 import BeautifulSoup
surnames= ["Amazon", "CAC40", "Facebook", "Bitcoin", "Apple"]
names= ["AMZN", "^FCHI", "FB", "BTC-EUR", "AAPL"]
def get_price():
r=requests.get('https://fr.finance.yahoo.com/quote/{}'.format())
soup=bs4.BeautifulSoup(r.text,'lxml')
price=soup.find('div',{'class':'My(6px) Pos(r) smartphone_Mt(6px)'}).find('span').next.next.next.text
问题:我不知道怎么做才能每次都更改名字,然后自动转到列表中的下一个
我觉得你想要的是
def pourcent(name, surname):
r = requests.get('https://fr.finance.yahoo.com/quote/{}'.format(name), data={'p': surname})
...
for name, surname in zip(names, surnames):
pourcent(name, surname)
您的函数需要使用一些参数来完成传递给requests.get
的URL,并且需要迭代列表,以便可以对每个必要的值对调用pourcent
。