[英]Need a try catch block
The function below works fine except if it scans the page and finds the tag "fullview-news-outer" does not exists.下面的 function 工作正常,除非它扫描页面并发现标签“fullview-news-outer”不存在。 This produces the error "list index out of range" .
这会产生错误“列表索引超出范围” 。 How can I do a try catch to make sure tag "fullview-news-outer" exists and if it does not exit else set the table variable accordingly.
如何尝试捕获以确保标签“fullview-news-outer”存在,如果它不退出,则相应地设置表变量。
def get_news2(ticker):
"""
Returns a list of sets containing news headline and url
"""
page_parsed, _ = http_request_get(url=STOCK_URL, payload={'t': ticker}, parse=True)
table = page_parsed.cssselect('table[class="fullview-news-outer"]')[0]
...
return (df)
as barmar said正如巴尔马尔所说
table = page_parsed.cssselect('table[class="fullview-news-outer"]')
if len(table) > 0:
tbl_first = table[0]
You can solve the problem without try-catch不用try-catch也能解决问题
page_parsed, _ = http_request_get(url=STOCK_URL, payload={'t': ticker}, parse=True)
selected = page_parsed.cssselect('table[class="fullview-news-outer"]')
if selected:
table = selected[0]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.