【发布时间】:2019-05-01 01:31:48
【问题描述】:
我编写的 for 循环有问题,我无法让 for 循环返回到第一个 for 语句:
def output(query,page,max_page):
"""
Parameters:
query: a string
max_page: maximum pages to be crawled per day, integer
Returns:
List of news dictionaries in a list: [[{...},{...}..],[{...},]]
"""
news_dicts_all = []
news_dicts = []
# best to concatenate urls here
date_range = get_dates()
for date in get_dates():
s_date = date.replace(".","")
while page < max_page:
url = "https://search.naver.com/search.naver?where=news&query=" + query + "&sort=0&ds=" + date + "&de=" + date + "&nso=so%3Ar%2Cp%3Afrom" + s_date + "to" + s_date + "%2Ca%3A&start=" + str(page)
header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'}
req = requests.get(url,headers=header)
cont = req.content
soup = BeautifulSoup(cont, 'html.parser')
for urls in soup.select("._sp_each_url"):
try:
if urls["href"].startswith("https://news.naver.com"):
news_detail = get_news(urls["href"])
adict = dict()
adict["title"] = news_detail[0]
adict["date"] = news_detail[1]
adict["company"] = news_detail[3]
adict["text"] = news_detail[2]
news_dicts.append(adict)
except Exception as e:
continue
page += 10
news_dicts_all.append(news_dicts)
return news_dicts_all
我已经执行了代码,似乎page += 将代码返回到“while”部分,但在页面到达max_page 后不会返回到for date in get_dates() 部分。
我想要的本质上是在到达max_page 后返回到for date in get_dates() 的代码,但我不知道如何才能完成这项工作。
【问题讨论】:
标签: python-3.x for-loop