使用 yield 时无法在多行中使用不同的值
Unable to use different values in multiple lines while using yield
我想弄清楚如何在不同的行中使用 yield
打印多个值。 更清楚:不使用多个yield
;相反,一个 yield
有多行 。在 return
的情况下,我可以使用这样的东西:
return (placeholder_one,placeholder_two,placeholder_three +
placeholder_four,placeholder_five,placeholder_six,title,link)
但是,在使用 yield
执行相同操作时,我遇到了困难。
我的目标是将值写入 csv 文件。如果我使用 return
,我可以按以下方式编写相同的内容:
placeholder_one,placeholder_two,placeholder_three,placeholder_four,placeholder_five,placeholder_six,title,link = fetch_items()
writer.writerow([placeholder_one,placeholder_two,placeholder_three,placeholder_four,placeholder_five,placeholder_six,title,link])
如果我使用 yield
,我可以简单地在 name 函数中使用它(这将是最理想的):
if __name__ == '__main__':
for item in fetch_items():
writer.writerow(item)
print(item)
我使用了一些占位符来使行变大;
import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin
base = "https://whosebug.com"
url = "https://whosebug.com/questions/tagged/web-scraping"
def fetch_items():
res = requests.get(url,headers={"User-Agent":"Mozilla/5.0"})
soup = BeautifulSoup(res.text,"html.parser")
placeholder_one = "Some name"
placeholder_two = "Some id"
placeholder_three = "Gender info"
placeholder_four = "Some phone"
placeholder_five = "Some email"
placeholder_six = "Some credit info"
for items in soup.select(".summary"):
title = items.select_one(".question-hyperlink").get_text(strip=True)
link = urljoin(base,items.select_one(".question-hyperlink").get("href"))
yield placeholder_one,placeholder_two,placeholder_three,placeholder_four,placeholder_five,placeholder_six,title,link
if __name__ == '__main__':
for item in fetch_items():
print(item)
我怎样才能像 return
那样 yield
两行或三行中的值?
尝试创建一个列表或元组以产生:
yield [placeholder_one,placeholder_two,placeholder_three + placeholder_four,placeholder_five,placeholder_six,title,link]
举个例子:
def num():
for i in range(10):
yield [2 + i, 6 + i, 7 + i]
for i in num():
print(i[0])
print(i[1])
print(i[2])
也看看yield-multiple-values
您是否期待接近于:
for items in soup.select(".summary"):
title = items.select_one(".question-hyperlink").get_text(strip=True)
link = urljoin(base,items.select_one(".question-hyperlink").get("href"))
yield (placeholder_one,placeholder_two,placeholder_three,
placeholder_four,placeholder_five,placeholder_six,
title,link)
我想弄清楚如何在不同的行中使用 yield
打印多个值。 更清楚:不使用多个yield
;相反,一个 yield
有多行 。在 return
的情况下,我可以使用这样的东西:
return (placeholder_one,placeholder_two,placeholder_three +
placeholder_four,placeholder_five,placeholder_six,title,link)
但是,在使用 yield
执行相同操作时,我遇到了困难。
我的目标是将值写入 csv 文件。如果我使用 return
,我可以按以下方式编写相同的内容:
placeholder_one,placeholder_two,placeholder_three,placeholder_four,placeholder_five,placeholder_six,title,link = fetch_items()
writer.writerow([placeholder_one,placeholder_two,placeholder_three,placeholder_four,placeholder_five,placeholder_six,title,link])
如果我使用 yield
,我可以简单地在 name 函数中使用它(这将是最理想的):
if __name__ == '__main__':
for item in fetch_items():
writer.writerow(item)
print(item)
我使用了一些占位符来使行变大;
import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin
base = "https://whosebug.com"
url = "https://whosebug.com/questions/tagged/web-scraping"
def fetch_items():
res = requests.get(url,headers={"User-Agent":"Mozilla/5.0"})
soup = BeautifulSoup(res.text,"html.parser")
placeholder_one = "Some name"
placeholder_two = "Some id"
placeholder_three = "Gender info"
placeholder_four = "Some phone"
placeholder_five = "Some email"
placeholder_six = "Some credit info"
for items in soup.select(".summary"):
title = items.select_one(".question-hyperlink").get_text(strip=True)
link = urljoin(base,items.select_one(".question-hyperlink").get("href"))
yield placeholder_one,placeholder_two,placeholder_three,placeholder_four,placeholder_five,placeholder_six,title,link
if __name__ == '__main__':
for item in fetch_items():
print(item)
我怎样才能像 return
那样 yield
两行或三行中的值?
尝试创建一个列表或元组以产生:
yield [placeholder_one,placeholder_two,placeholder_three + placeholder_four,placeholder_five,placeholder_six,title,link]
举个例子:
def num():
for i in range(10):
yield [2 + i, 6 + i, 7 + i]
for i in num():
print(i[0])
print(i[1])
print(i[2])
也看看yield-multiple-values
您是否期待接近于:
for items in soup.select(".summary"):
title = items.select_one(".question-hyperlink").get_text(strip=True)
link = urljoin(base,items.select_one(".question-hyperlink").get("href"))
yield (placeholder_one,placeholder_two,placeholder_three,
placeholder_four,placeholder_five,placeholder_six,
title,link)